Fetch next not working

  • I'm running this statemment and it stay in loop forever do not check end of file!! any ideas? thanks

    CREATE PROCEDURE proc6 AS

    delete from billable_temp

    DECLARE @col001 char(10)

    DECLARE csrSites CURSOR FOR

    SELECT col001 FROM junkgood

    OPEN csrSites

    FETCH NEXT FROM csrSites INTO @col001

    WHILE @@FETCH_STATUS = 0

    BEGIN

    INSERT INTO billable_temp (otrunkid)

    SELECT substring(col001,10,7)

    FROM junkgood

    FETCH NEXT FROM csrSites INTO @col001

    END

    SELECT *

    FROM billable_temp

    CLOSE csrSites

    DEALLOCATE csrSites

    GO

  • While this shouldn't loop forever, I imagine you actually want something different. First, you are not using the value read by the cursor. The statement of INSERT INTO... will read from the entire table of junkgood for each row. It probably seems like the program is looping forever because of the duplicates. Which brings me to the second point. Unless you are actually doing a lot of other specific processing, you can remove all the cursor code and just execute the INSERT INTO statement. It is much preferred to just use the entries as a batch.

    Hope this clarifies things.

    Guarddata-

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply