sql 循环处理查询到的结果集

方法一:游标

DECLARE

@formid INT
BEGIN
DECLARE c_test CURSOR FAST_FORWARD FOR
SELECT formid FROM [biz_HrPerformCountermeasureBasic];
OPEN c_test;
WHILE 1=1
BEGIN
FETCH NEXT FROM c_test INTO @formid;
IF @@fetch_status!= 0 BREAK;
exec mSP_AddHrPerformToTask @formid
END;
CLOSE c_test
DEALLOCATE c_test

END;

方法二:while

Declare @i int
Select @i=Min(FormID) from   [K2BPM].[dbo].[biz_HrPerformCountermeasureBasic] 


While @i Is Not Null
Begin
exec mSP_AddHrPerformToTask_Import @i

Select @i=Min(FormID) from   [K2BPM].[dbo].[biz_HrPerformCountermeasureBasic]  Where FormID>@I
 
End
  

你可能感兴趣的:(SQL,Server)