--开始事务
BEGIN TRAN

--不显示计数信息
SET NOCOUNT ON

declare @companyid int
declare @personid  int



declare  OuterCursor  cursor
for   
   --tim
   select personid,companyid from company where employeeid in(2000000043)
open OuterCursor
      fetch next from OuterCursor into @personid,@companyid
      while(@@fetch_status=0)
          begin
             -- do work
                declare @userid int
                set @userid=null
                 
                select top 1 @userid=appuserid 
                      from  appuser_usergroup  where
                          groupid in( select  groupid  from usergroup_company uc
                                                       where uc.companyid=@personid
                                     )
                
                 if @userid  is null
                   begin
                        update company set employeeid = null where companyid=@companyid 
                   end
                 else
                    begin
                        print @userid
                        update company set employeeid = @userid where companyid=@companyid              
                    end
              -- do work end
                 
              fetch next from OuterCursor into @personid,@companyid
          end

             --执行错误回滚
           if @@error!=0
           begin
             rollback tran
             return
           end
 
            --提交所有变更
            COMMIT TRAN

close OuterCursor
deallocate OuterCursor

posted on 2011-01-24 15:05  tim123  阅读(225)  评论(0编辑  收藏  举报