美文网首页
sql server循环的2种样板代码

sql server循环的2种样板代码

作者: 吉凶以情迁 | 来源:发表于2025-01-18 13:48 被阅读0次
   SELECT ROW_NUMBER() OVER (ORDER BY Name) id, Name
        INTO #MpsB
DECLARE @counter INT = 1;
DECLARE @maxCount INT = (SELECT COUNT(*) FROM #MpsB);

WHILE @counter <= @maxCount
BEGIN
select * from #MpsB where id=counter 
    SET @counter = @counter + 1;
END;



方法2:

declare @id int ;
declare @name varchar(100)='';
DECLARE cursor_name CURSOR FOR
SELECT ID,Name FROM YourTable;

OPEN cursor_name;

FETCH NEXT FROM cursor_name INTO @id,name;

WHILE @@FETCH_STATUS = 0
BEGIN
    EXEC YourStoredProcedure @id;
    FETCH NEXT FROM cursor_name INTO @id,@name;
END;

CLOSE cursor_name;
DEALLOCATE cursor_name;

相关文章

网友评论

      本文标题:sql server循环的2种样板代码

      本文链接:https://www.haomeiwen.com/subject/jfoayjtx.html