How deep can you nest cursors?

  • I know cursors are frownded upon but since this is for an infrequent utility I'm not overly worried about any possible performance ramifications.

    I have a need to copy the values from a number of feeder tables from one database to another.

    I started by creating a table of tablenames. Going from there, I loop thru the tablenames and determine all the column names. Then I need to loop thru the column names and create the appropriate update statement.

    I use one cursor to go thru the tablenames, one cursor to go thru sysobjects/syscolumns to get the column names. The problem is that I cannot seem to create a 3rd nested cursor where I go thru the collection of column names to build the update statements. It cannot see previously declared variables regardless of where I declare them.

    I've looked around and while I cannot see anything that says you cannot have more than 'x' number of cursors any example I've ever seen only seems to have 2, so I guess thats my question/problem.

    Regards,

    Chris

  • This was removed by the editor as SPAM

  • Can you post the code for the sproc? I've only ever seen cursors nested 2 deep as well, but I don't see any reason why you couldn't go deeper...

    The only thing I can think of off the top of my head is that you're building some dynamic SQL somewhere in there that's trying to make use of variables declared in an outer scope. Depending on the implementation, those variables may not be seen by the dynamic SQL.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply