1. 程式人生 > >一個巢狀遊標的例子

一個巢狀遊標的例子

declare @class varchar(30)
declare classcursor cursor for
select class from cmrtxl19.qifei.class where cmrtxl19.qifei.class.class like '%北京0609%'
open classcursor
fetch next from classcursor into @class
while ( @@fetch_status=0 ) 
begin
    print @class
    declare @username varchar(30)
    declare @userid varchar(30)
    declare classcursor_2 cursor for
    select 姓名,使用者名稱 from pubs.qifei.bj0609fenban where 序號 is null and 班級名稱
[email protected]

    open classcursor_2
    fetch next from classcursor_2 into @username,@userid
    declare @i int
    set @i=1
    while ( @@fetch_status=0) 
    begin
        print '----'[email protected][email protected]
        if @i=1
        begin
            update cmrtxl19.qifei.class set adm = @username,admid = @userid
            where cmrtxl19.qifei.class.class = @class
        end
        else
        begin
            update cmrtxl19.qifei.class set adm2 = @username,adm2id = @userid
            where cmrtxl19.qifei.class.class = @class
        end
        set @
[email protected]
+1
        if @i=3
        begin
            break
        end
        fetch next from classcursor_2 into @username,@userid
    end                       
    close classcursor_2
    deallocate classcursor_2
    fetch next from classcursor into @class
end
close classcursor
deallocate classcursor