I have 3 cursors: DECLARE cursor1 CURSOR FOR SELECT @intVar1 = Col1 FROM Table1;
DECLARE cursor2 CURSOR FOR SELECT @intVar2 = Col1 FROM Table2 WHERE Col2 = @intVar1
DECLARE cursor2 CURSOR FOR SELECT @intVar3 = Col1 FROM Table3 WHERE Col2 = @intVar1
Basically I want to retrieve each Col1 into @intVar1 [using cursor1], then use that to pluck out corresponding rows using cursor2 and cursor3. Is this possible in SQLServer 2005?
asked Mar 07 '10 at 03:16 PM in Default
I know you've solved your answer, but two things -
1) I hope your solution involved ditching the cursors and using set based code
2) Seeing as my hopes are likely to be dashed, please can you post your solution here such that someone who finds this page from google in the future finds some useful information...
answered Mar 07 '10 at 07:48 PM
Matt Whitfield ♦♦
I too hope that the cursors were ditched. If the solution were to be posted, then it is highly likely that somebody here can offer an alternative solution to meet your requirements.
answered Mar 07 '10 at 11:01 PM
Do you have to use a cursor? There are other solutions to your question, (depending on what you are doing to the @intVar2 and @intVar3) Joins, sub queries, in, exists, etc.
I am not a fan of cursors and that is why I post this answer to give you something to consider. Cursors are usually very slow in SQL server, BUT sometimes a cursor outperforms a bad set based statement.
answered Mar 09 '10 at 06:12 AM