开发者

for loop in Sql procedure

开发者 https://www.devze.com 2023-01-10 19:56 出处:网络
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 be

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

精彩评论

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