I have a database with two tables, Comics and Summary. Comics contains a list of 1000s of individual issues with each record corresponding to an individual issue, and the columns include a name and an issue number (amongst other things). The Summary table is currently empty but once I get the stored procedure running will contain a list in of all the titles in Comics, with record corresponding to a title, and include a columns name and summary. In the Summary table, the purpose of the summary column is to express a single text string which issues are available for an individual title. The purpose of the stored procedure is to build the Summary table from Comics. In other words, to use a cursor to iterate through the Comics and populate Summary accordingly.
For example, say that Comics has the following records:
Comic A 1
Comic A 2
Comic A 3
Comic A 5
Comic B 2
Comic B 4
Comic B 5
Comic B 6
Then the stored procedure would insert the following to the Summary table:
Comic A 1-3, 5
Comic B 2, 4-6
I have written a stored procedure that I thought would more or less work, perhaps a bit of fine tuning, but would get me most of the way there. The strange is that when I run the stored procedure, it doesn't insert even a single row into the Summary table, the table remains completely empty regardless. I attempt to run the SP by right clicking on it and selecting "Execute Stored Procedure". I'm not quite sure what to do. If it were inserting records incorrectly, it would just a matter of adjusting some of the logic, but as such there is nothing to go on. Can you see anything wrong with my code?
USE [comics] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].[ComicList] AS DECLARE @name VARCHAR(50) DECLARE @issue INT DECLARE @prevname VARCHAR(50) DECLARE @previssue INT DECLARE @summary VARCHAR(200) DECLARE @rangestart INT BEGIN TRY SET NOCOUNT ON; SET @summary = '' DECLARE com_cursor CURSOR FAST_FORWARD FOR SELECT Name, Issue FROM Comics ORDER BY Name, Issue OPEN com_cursor FETCH NEXT FROM com_cursor INTO @name, @issue SET @prevname = @name SET @previssue = @issue - 1 SET @rangestart = @issue WHILE @@FETCH_STATUS = 0 BEGIN IF (@name = @prevname) BEGIN IF (@issue <> @previssue + 1) BEGIN IF (@rangestart < @previssue) BEGIN IF (LEN(@summary) > 0) BEGIN SET @summary = @summary + ', ' + @rangestart + '-' + @previssue END ELSE BEGIN SET @summary = @rangestart + '-' + @previssue END END ELSE BEGIN IF (LEN(@summary) > 0) BEGIN SET @summary = @summary + ', ' + @rangestart END ELSE BEGIN SET @summary = @rangestart END END SET @rangestart = @issue END END ELSE BEGIN IF (@rangestart < @previssue) BEGIN IF (LEN(@summary) > 0) BEGIN SET @summary = @summary + ', ' + @rangestart + '-' + @previssue END ELSE BEGIN SET @summary = @rangestart + '-' + @previssue END END ELSE BEGIN IF (LEN(@summary) > 0) BEGIN SET @summary = @summary + ', ' + @rangestart END ELSE BEGIN SET @summary = @rangestart END END INSERT INTO Summary (Name, Summary) VALUES (@prevname, @summary) SET @summary = '' SET @rangestart = @issue END SET @prevname = @name SET @previssue = @issue FETCH NEXT FROM com_cursor INTO @name, @issue END CLOSE com_cursor DEALLOCATE com_cursor END TRY BEGIN CATCH PRINT 'There was an error. Check to make sure object exists.' IF CURSOR_STATUS('variable', 'col_cursor') <> -3 BEGIN CLOSE com_cursor DEALLOCATE com_cursor END END CATCH GO