Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8562739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:47:45+00:00 2026-06-11T16:47:45+00:00

In my sql code I have declared a table(A) and inserting some rows to

  • 0

In my sql code I have declared a table(A) and inserting some rows to that table from a database table(B). Then I have to take those inserted rows from A and put it into a CURSOR and after I do a FETCH NEXT still the @@FETCH_STATUS is -1. But the expected value for the @@FETCH_STATUS is 0.
I am putting a simplified code below the question.

Can I know what is the wrong with this code. Can I use declared tables to populate the CURSORs in SQL or cursor has to be populated from a created table in the database.

// This is a code that goes inside a Stored Procedure.

AS
   DECLARE A TABLE (.........)// A table has same fields in table B
   DECLARE s INT

   WHILE EXISTS ( SELECT * FROM B WHERE ......)
   BEGIN
      BEGIN TRAN
      INSERT INTO A SELECT TOP 10 (....)FROM B WITH (UPDLOCK, HOLDLOCK) WHERE ....

      SELECT s = count(*) from A // this returns some value which means inserting is working 
      DECLARE  dataSet CURSOR FOR (SELECT..... FROM A)
      OPEN dataSet
      FETCH NEXT FROM dataSet INTO ...

       WHILE @@FETCH_STATUS = 0 // coming value for this is -1
       BEGIN
          //Code goes here
          FETCH NEXT FROM dataSet INTO ...
        END

   CLOSE dataSet
   DEALLOCATE dataSet
   DELETE FROM A
   COMMIT TRAN
END
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:47:47+00:00Added an answer on June 11, 2026 at 4:47 pm

    Here’s a runnable version of the OPs question – but it doesn’t exhibit the issue. This isn’t an answer, so CW, and I’ll delete if/when the OP does post an actual example:

    create table B (ID int not null,Val1 varchar(10) not null)
    go
    insert into B(ID,Val1) values (1,'abc'),(2,'ade')
    go
    create procedure DoStuff
    AS
       DECLARE @A TABLE (ID int not null,Val1 varchar(10) not null)
       DECLARE @s INT
    
       WHILE EXISTS ( SELECT * FROM B WHERE Val1 like 'a%')
       BEGIN
          BEGIN TRAN
          INSERT INTO @A SELECT TOP 10 ID,Val1 FROM B WITH (UPDLOCK, HOLDLOCK) WHERE Val1 like 'a%'
    
          SELECT @s = count(*) from @A
          DECLARE dataSet CURSOR FOR (SELECT ID,Val1 FROM @A)
          declare @ID int
          declare @Val1 varchar(10)
          OPEN dataSet
          FETCH NEXT FROM dataSet INTO @ID,@Val1
    
           WHILE @@FETCH_STATUS = 0
           BEGIN
              RAISERROR('%i: %s',10,1,@ID,@Val1) WITH NOWAIT
              UPDATE B set Val1 = 'done' where ID = @ID
              FETCH NEXT FROM dataSet INTO @ID,@Val1
            END
    
       CLOSE dataSet
       DEALLOCATE dataSet
       DELETE FROM @A
       COMMIT TRAN
    END
    GO
    EXEC DoStuff
    GO
    SELECT * from B
    

    Output:

    (2 row(s) affected)
    
    (2 row(s) affected)
    1: abc
    
    (1 row(s) affected)
    2: ade
    
    (1 row(s) affected)
    
    (2 row(s) affected)
    
    (2 row(s) affected)
    

    and table B:

    ID  Val1
    1   done
    2   done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following SQL code that runs against a Change Request database. Each
I have this code to update my SQL database from data in a textbox,
I have some code that uses the SQL Server 2005 SMO objects to backup
I have some PL/SQL code that I think might have a memory leak. Everytime
In my Java code I have embedded a SQL query which fetches data from
enter code here I have a table on SQL server 2005 with bigint primary
I have C# code that cycles through .sql files and executes what's inside them
I have the code below: string SQL = select * from + TableName; using
I have the following t-sql code which generates an error Declare @table TABLE (
I am working on sql server monitoring product and i have database query that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.