现在的位置: 首页 > 综合 > 正文

SQL 游标示例

2012年11月26日 ⁄ 综合 ⁄ 共 510字 ⁄ 字号 评论关闭
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(1from 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

 

抱歉!评论已关闭.