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 723895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:09:37+00:00 2026-05-14T06:09:37+00:00

I have a stored procedure DECLARE cursor FOR SELECT [FooData] From [FooTable]; OPEN cursor

  • 0

I have a stored procedure

DECLARE cursor FOR SELECT [FooData] From [FooTable];
OPEN cursor ;  
FETCH NEXT FROM cursor INTO @CurrFooData;
WHILE @@FETCH_STATUS = 0
BEGIN
  SELECT @CurrFooData AS FooData;
  INSERT INTO Bar (BarData) VALUES(@CurrFooData);
  FETCH NEXT FROM cursor INTO @CurrFooData;
END;
CLOSE cursor 
DEALLOCATE cursor 

But in result I have a lot of tables, not one. How can I return one table with ‘FooData’ column and all ‘@CurrFooData’ rows?

  • 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-05-14T06:09:38+00:00Added an answer on May 14, 2026 at 6:09 am

    Dmitry, I think you should really try to get rid of that cursor all together. In the second example, you’re selecting two fields FooData1 and FooData2, but in the end, you’re only ever inserting of the values….

    You could rewrite that second query easily – something like:

    INSERT INTO Bar (BarData) 
        SELECT FooData1 FROM FooTable
    

    That’s all that your entire cursor and everything is really doing.

    Try to avoid cursors at all costs – 99% of the time, they’re not needed – start to think in SETS of data – do not stick to procedural loops and cursor in SQL – that’s just not a good match!

    If you need to output what you’ve just inserted, use the OUTPUT clause:

    INSERT INTO Bar (BarData) 
        OUTPUT Inserted.* 
        SELECT FooData1 FROM FooTable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have this great query in my stored procedure. Select * from Temp
I have following stored procedure: CREATE PROCEDURE testProc(IN p_idProject INTEGER) BEGIN DECLARE nowTime DATETIME;
I have a stored procedure in oracle which is using cursor.Its fetching the data
I have a stored procedure which returns sys_refcursor and I am trying to fetch
I have the following statement in a stored procedure: DECLARE @Count INT EXEC @Count
I have a stored proc which inserts rows from a view with ranks into
I have a stored procedure. I want to call a function from it. Want
I have a stored procedure simplified to the following code: SET NOCOUNT ON DECLARE
I Have a cursor in stored procedure under SQL Server 2000 (not possible to
I have created a stored procedure to delete data from multiple tables. my work

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.