sql游标循环

时间:2021-04-17 19:33:53
DECLARE @begindate DATETIME=CONVERT(DATETIME, '2019.05.06 09:40:50')
,@enddate DATETIME =GETDATE()
,@id INT DECLARE My_Cursor CURSOR --定义游标
FOR (SELECT id FROM dbo.op_weight_cemskind_charge where ddate between @begindate and @enddate) --查出需要的集合放到游标中
OPEN My_Cursor; --打开游标
FETCH NEXT FROM My_Cursor INTO @id;--读取第一个游标值赋值给变量
WHILE @@FETCH_STATUS = 0 --游标值是否已经读取完毕
BEGIN
SELECT * FROM dbo.op_weight_cemskind_charge WHERE id=@id
FETCH NEXT FROM My_Cursor INTO @id;
END
CLOSE My_Cursor; --关闭游标
DEALLOCATE My_Cursor; --释放游标