Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. @@FETCH_STATUS

@@FETCH_STATUS

Scheduled Pinned Locked Moved Database
databasequestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sroberts82
    wrote on last edited by
    #1

    I have a stored procedure that has two cursors running one after another. I say while @@fetch_status = 0 to enter the loop. when the first one finishes fetch_status is -1 and wont enter the 2nd cursor. can i manually set fetch_status? Thanks in advance Stephen

    D 1 Reply Last reply
    0
    • S sroberts82

      I have a stored procedure that has two cursors running one after another. I say while @@fetch_status = 0 to enter the loop. when the first one finishes fetch_status is -1 and wont enter the 2nd cursor. can i manually set fetch_status? Thanks in advance Stephen

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Let's see the code. It sounds like you haven't "primed the pump" so to speak. The way to do it is to setup your cursor, read the first record, THEN enter the loop controled by @@FETCH_STATUS. Something like this:

      DECLARE curMasterGames CURSOR FOR
      SELECT MasterGameID FROM MasterGames
      

      OPEN curMasterGames
      DECLARE @GameID bigint
       
      -- Prime the pump.
      FETCH NEXT FROM curMasterGames INTO @GameID
      -- Check to see if the FETCH failed on the first record BEFORE we try
      -- to do any processing on that records data.
      WHILE @@FETCH_STATUS = 0
      BEGIN
      -- It seems the FETCH worked, do something with it.
      EXEC UpdatePlayerScoresOnMasterGameID @GameID
      -- Get the next record.
      FETCH NEXT FROM curMasterGames INTO @GameID
      END
      CLOSE curMasterGames
      DEALLOCATE curMasterGames
       
      -- Do the same exact pattern for the next cursor you open.
      -- Prime, test for failure, then process your loop.

      RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome -- modified at 10:37 Friday 20th January, 2006

      S M 2 Replies Last reply
      0
      • D Dave Kreskowiak

        Let's see the code. It sounds like you haven't "primed the pump" so to speak. The way to do it is to setup your cursor, read the first record, THEN enter the loop controled by @@FETCH_STATUS. Something like this:

        DECLARE curMasterGames CURSOR FOR
        SELECT MasterGameID FROM MasterGames
        

        OPEN curMasterGames
        DECLARE @GameID bigint
         
        -- Prime the pump.
        FETCH NEXT FROM curMasterGames INTO @GameID
        -- Check to see if the FETCH failed on the first record BEFORE we try
        -- to do any processing on that records data.
        WHILE @@FETCH_STATUS = 0
        BEGIN
        -- It seems the FETCH worked, do something with it.
        EXEC UpdatePlayerScoresOnMasterGameID @GameID
        -- Get the next record.
        FETCH NEXT FROM curMasterGames INTO @GameID
        END
        CLOSE curMasterGames
        DEALLOCATE curMasterGames
         
        -- Do the same exact pattern for the next cursor you open.
        -- Prime, test for failure, then process your loop.

        RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome -- modified at 10:37 Friday 20th January, 2006

        S Offline
        S Offline
        sroberts82
        wrote on last edited by
        #3

        Nice one, thats exactly the problem, thank you ver much

        1 Reply Last reply
        0
        • D Dave Kreskowiak

          Let's see the code. It sounds like you haven't "primed the pump" so to speak. The way to do it is to setup your cursor, read the first record, THEN enter the loop controled by @@FETCH_STATUS. Something like this:

          DECLARE curMasterGames CURSOR FOR
          SELECT MasterGameID FROM MasterGames
          

          OPEN curMasterGames
          DECLARE @GameID bigint
           
          -- Prime the pump.
          FETCH NEXT FROM curMasterGames INTO @GameID
          -- Check to see if the FETCH failed on the first record BEFORE we try
          -- to do any processing on that records data.
          WHILE @@FETCH_STATUS = 0
          BEGIN
          -- It seems the FETCH worked, do something with it.
          EXEC UpdatePlayerScoresOnMasterGameID @GameID
          -- Get the next record.
          FETCH NEXT FROM curMasterGames INTO @GameID
          END
          CLOSE curMasterGames
          DEALLOCATE curMasterGames
           
          -- Do the same exact pattern for the next cursor you open.
          -- Prime, test for failure, then process your loop.

          RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome -- modified at 10:37 Friday 20th January, 2006

          M Offline
          M Offline
          Mike Ellison
          wrote on last edited by
          #4

          Hi Dave. I love your responses to CP questions. Always thorough and informative. Just thought I'd offer you a virtual :beer: for your efforts.

          D 1 Reply Last reply
          0
          • M Mike Ellison

            Hi Dave. I love your responses to CP questions. Always thorough and informative. Just thought I'd offer you a virtual :beer: for your efforts.

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            Hey, Thanks! I'm glad to hear to it! But, I'm not always this way! :-D I can get rushed by my job and just don't have the time to put in a full answer (maybe I shouldn't answer then :-D), but I try. I also have lots of room for improvement. I can get pretty short with some people. Specially when they ask a question like "what's wrong with the object I'm holding behind my back?" and don't give ANY details about the code or error message. I swear, I'm just waiting for the post that says "My app doesn't work." and that's it, nothing else! :laugh: But, if the question is a good one and all the necessary tidbits are there to figure out what they're trying to do with the code, I'll take the time to dig up the details of the answer and come up with an example if I can. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            M 1 Reply Last reply
            0
            • D Dave Kreskowiak

              Hey, Thanks! I'm glad to hear to it! But, I'm not always this way! :-D I can get rushed by my job and just don't have the time to put in a full answer (maybe I shouldn't answer then :-D), but I try. I also have lots of room for improvement. I can get pretty short with some people. Specially when they ask a question like "what's wrong with the object I'm holding behind my back?" and don't give ANY details about the code or error message. I swear, I'm just waiting for the post that says "My app doesn't work." and that's it, nothing else! :laugh: But, if the question is a good one and all the necessary tidbits are there to figure out what they're trying to do with the code, I'll take the time to dig up the details of the answer and come up with an example if I can. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

              M Offline
              M Offline
              Mike Ellison
              wrote on last edited by
              #6

              I know the feeling - all around :)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups