i want to implement for loop to fetch the data from select statement.
A:
hgulyan
2010-08-07 10:33:26
To which I'd say: just DON'T - don't even get started with cursors - do not use cursor, they're a) unnecessary, b) a performance killer, and c) evil
marc_s
2010-08-07 17:20:52
You're right, but in some cases, especially if it's one time action, cursor is the only(or the best) solution.
hgulyan
2010-08-07 18:05:18
A:
Your asking abour cursors, but cursors are evil, because they have a bad performance. Mosts of times there is a better aproach to solve the problem without using it. But if you still want to do it, here is a very simple snippet of code.
DECLARE @somevariable VARIABLE_TYPE_HERE
DECLARE @sampleCursor CURSOR
SET @sampleCursor = CURSOR FOR
SELECT somefield... from bla bla bla...
OPEN @sampleCursor
FETCH NEXT
FROM @sampleCursor INTO @somevariable
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @somevariable
FETCH NEXT
FROM @sampleCursor INTO @somevariable
END
CLOSE @sampleCursor
DEALLOCATE @sampleCursor
Jonathan
2010-08-07 10:43:45