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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:43:12+00:00 2026-06-05T18:43:12+00:00

Does any body know how to set different select statement for the same cursor?

  • 0

Does any body know how to set different select statement for the same cursor? I need somethink like this.

DECLARE Temp_Cursor CURSOR FOR
IF(@TempVar = 1)
BEGIN
    SELECT CustomerId FROM Customers
END
ELSE IF(@TempVar = 2)
BEGIN
    SELECT OrderId FROM Orders
END
OPEN Temp_Cursor;
FETCH NEXT FROM TempCursor INTO @TempObjectId
WHILE @@FETCH_STATUS = 0
BEGIN 

…. etc

I have found solution: DECLARE @Temp_Cursor CURSOR
IF(@TempVar = 1)
BEGIN
SET @Temp_Cursor = CURSOR FOR
SELECT CustomerId FROM Customers
END
ELSE IF(@TempVar = 2)
BEGIN
SET @Temp_Cursor = CURSOR FOR
SELECT OrderId FROM Orders
END
OPEN @Temp_Cursor;
FETCH NEXT FROM @TempCursor INTO @TempObjectId
WHILE @@FETCH_STATUS = 0
BEGIN

  • 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-05T18:43:14+00:00Added an answer on June 5, 2026 at 6:43 pm

    Put the IF outside and do two different cursors, one for each situation.

    Like this:

    IF(@TempVar = 1)
    BEGIN
    
        DECLARE Temp_Cursor CURSOR FOR
        SELECT CustomerId FROM Customers
    
    
        OPEN Temp_Cursor;
        FETCH NEXT FROM TempCursor INTO @TempObjectId
        WHILE @@FETCH_STATUS = 0
        BEGIN 
        ....
    END
    ELSE IF(@TempVar = 2)
    BEGIN
    
        DECLARE Temp_Cursor CURSOR FOR
        SELECT OrderId FROM Orders
    
    
        OPEN Temp_Cursor;
        FETCH NEXT FROM TempCursor INTO @TempObjectId
        WHILE @@FETCH_STATUS = 0
        BEGIN 
        ....
    END
    

    Another possibility is to use Dynamic SQL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does any body know how can I use MS_MPI in my VC++ MFC project?
Can two CPUs hold two different spin locks simultaneously at same time? So...does this
Does anybody know how to set the title to the form viewer when showing
Does anybody know how to set the Identity of the Mobile Device Management (MDM)
Does anybody know a basic plugin/gem that will allow me to quickly set up
Does anybody know whether one exists? I've been googling this for monthes... Thanks
Does anyone know if it's possible to use Ninject to resolve any unresolved abstract
Does anybody know how to center align a DIV that has the display set
Does anybody know how to debug call to undefined function init_set , when trying
Does anybody know of or have a detailed list of how the BCL in

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.