游标的使用

DECLARE My_Cursor1 CURSOR --定义游标
FOR (SELECT RowGuid , ProjectRowGuid FROM [BIMPM_Base].[dbo].[ProjectPermission_Role] where SubSystemRowGuid = 'A9318399-66FE-423E-93F5-CCD7C0A471C4') --查出需要的集合放到游标中
OPEN My_Cursor1; --打开游标
declare @RoleRowGuid varchar(50),@ProjectRowGuid varchar(50)
fetch next from My_Cursor1 into @RoleRowGuid,@ProjectRowGuid
while(@@FETCH_STATUS=0)
begin
---内层循环
declare Cursor22 CURSOR for (select RowGuid from [BIMPM_Base].[dbo].[ProjectPermissionItem] where SubSystemRowGuid = 'A9318399-66FE-423E-93F5-CCD7C0A471C4')
OPEN Cursor22
declare @ItemRowGuid varchar(50)
fetch next from Cursor22 into @ItemRowGuid
while(@@FETCH_STATUS=0)
begin
Insert into [BIMPM_Base].[dbo].[ProjectPermission_MenuRole](ProjectRowGuid,PermissionItemRowGuid,RoleRowGuid,SubSystemRowGuid)
values(@ProjectRowGuid,@ItemRowGuid,@RoleRowGuid,'A9318399-66FE-423E-93F5-CCD7C0A471C4')
fetch next from Cursor22 into @ItemRowGuid
end
close Cursor22

fetch next from My_Cursor1 into @RoleRowGuid,@ProjectRowGuid ---移动游标
end
close My_Cursor1

原文地址:https://www.cnblogs.com/wangzuofei/p/5159500.html