|
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?
(comments are locked)
|
|
I know you've solved your answer, but two things - 1) I hope your solution involved ditching the cursors and using set based code And 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... +1 Valid points. I hope the code gets posted.
Mar 07 '10 at 11:02 PM
CirqueDeSQLeil
+1 Admit it, Matt. You want him to post the solution because the triple-cursor is eating away at you. You're just dying to convert it. I don't blame you. :)
Mar 08 '10 at 01:00 AM
Tom Staab
(comments are locked)
|
|
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. DECLARE cursor1 CURSOR FOR SELECT Col1 FROM Table1; Open cursor1 Fetch cursor1 into @intVar1; while (blah blah blah) { DECLARE cursor2 CURSOR FOR SELECT Col1 FROM Table2 WHERE Col2 = @intVar1 DECLARE cursor3 CURSOR FOR SELECT Col1 FROM Table3 WHERE Col2 = @intVar1 } close cursor 1; deallocate cursor 1;
Mar 08 '10 at 08:37 AM
Ahmad Zaman
It looks more readable in the edit field
Mar 08 '10 at 08:38 AM
Ahmad Zaman
btw, how do you close a question?
Mar 08 '10 at 08:38 AM
Ahmad Zaman
Either check the "answered" checkbox by one of the answers or (in this case) add your own answer and then mark it as the answer.
Mar 09 '10 at 11:14 AM
Brimstedt
(comments are locked)
|
|
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.
OR
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.
(comments are locked)
|


Its Ok, solved the problem...just didn't realise how messy T-SQL could be!
If this is solved, please close the question.
Please add your solution so that others can benefit from your investigations. You may also find that you will have suggestions given to you on how you may be able to consider other options - cursors are very hard on your server performance, 3 of them doubly so.
Tom Staab wrote "If this is solved, please close the question."
Heh... if the op is still using a cursor, then this thread should never be closed. He should continue to give us updates on how slow and how many resources his code is using. ;-)