开发者

for loop in Sql procedure

i want to implement for loop 开发者_开发知识库to fetch the data from select statement.


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 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜