Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I set one cursor equal to another in a stored proc?

Status
Not open for further replies.

ddiamond

Programmer
Apr 22, 2005
918
US
I have 2 static cursors, WITHOUT_UNITS_CUR and WITH_UNITS_CUR. Depending on the variable, V_INCLUDE_UNITS, I want to use one or the other. Currently, I am using an if statement everytime I need to reference one of the cursors. For example:
Code:
IF V_INCLUDE_UNITS = 0 THEN
  OPEN WITHOUT_UNITS_CUR ;
  FETCH WITHOUT_UNITS_CUR INTO V_NIMANU , V_NIMASE , V_NINACD , V_NINUCD ;
ELSE
  OPEN WITH_UNITS_CUR ;
  FETCH WITH_UNITS_CUR INTO V_NIMANU , V_NIMASE , V_NINACD , V_NINUCD ;
END IF ;
What I'd love to be able to do is assign a 3rd cursor to one of them once, and then use that 3rd cursor instead of constantly repeating the if-else block. For example:
Code:
IF V_INCLUDE_UNITS = 0 THEN
  MY_CUR = WITHOUT_UNITS_CUR ;
ELSE
  MY_CUR = WITH_UNITS_CUR ;
END IF ;

OPEN MY_CUR ;
FETCH MY_CUR INTO V_NIMANU , V_NIMASE , V_NINACD , V_NINUCD ;
Is that possible?
 
Dan,
Don't think you can do what you want in the way that you are describing, although, as always, happy to be told I'm wrong.

What might be more possible, although haven't seen cursors obviously, is to combine both cursors into one, with a clever CASE statement.

Just a thought. Any chance?

Marc
 
Marc,

Yes I can combine them into one cursor. In fact that's how I originally coded it. My only problem was performance. The without_units cursor is much faster than the with_units cursor. Since 80% of the time the user doesn't want the units, I thought it was worth creating a separate cursor, without_units, that performed faster for that scenerio.

I also explored using a dynamic cursor, but that again took a performance hit because the system doesn't appear to cache dynamic sql.

- Dan
 
Dan,
My next suggestion was going to be dynamic SQl but it looks like you're way ahead of me and have already ruled it out.

Think you are stuck with the if-else block.

Marc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top