SQL 游标示例
declare @user varchar(20);
declare pcurr cursor for
select user from table1 where group_code = @group_code;
open pcurr
fetch next from pcurr into @user
while (@@fetch_status = 0)
begin
select @num = count(1) from table2 where user_code= @user and role_code=@role_code;
if @num <= 0
begin
insert into table2(user_code, role_code)
values(@user, @role_code);
end
fetch next from pcurr into @user
end
close pcurr
deallocate pcurr
declare pcurr cursor for
select user from table1 where group_code = @group_code;
open pcurr
fetch next from pcurr into @user
while (@@fetch_status = 0)
begin
select @num = count(1) from table2 where user_code= @user and role_code=@role_code;
if @num <= 0
begin
insert into table2(user_code, role_code)
values(@user, @role_code);
end
fetch next from pcurr into @user
end
close pcurr
deallocate pcurr