存储过程游标及WHILE语句

不想你离开。 提交于 2020-02-20 00:53:30

Declare Write_Cursor Cursor For

 Select a.AuditingID, a.BillType, a.AuditingDescription, a.AuditingStep, a.EtypeID, b.FullName, a.AuditingAlerm
 From AuditingFlowDetail a,Employee b
 Where  a.AuditingID = @nAuditingID
   And a.AuditingStep = @CurStep
   And a.EtypeID = b.TypeID
   AND outKty=@IfOut
 
 Open Write_Cursor
 
 Fetch Next From Write_Cursor Into @AuditingID, @BillType, @AudDesc, @AuditingStep, @ETypeID, @EName, @AuditingAlerm


 IF @IfOut='i' AND @OutLever<>1
 BEGIN
  Insert #AudDetail  ([AudID],[BillType],[Desc],[Step],[EtypeID],[Name],[AuditingAlerm])
      Values (@AuditingID, @BillType, @AudDesc, @AuditingStep-@OutLever+1,',',',', @AuditingAlerm)
  While @@Fetch_status = 0
 
  Begin
   Update #AudDetail
   Set [ETypeID] = [ETypeID] + @ETypeID + ',', [Name] = [Name] + @EName + ','
   Where Step = @CurStep-@OutLever+1
 
   Fetch Next From Write_Cursor Into @AuditingID, @BillType, @AudDesc, @AuditingStep, @ETypeID,  @EName, @AuditingAlerm
  END
 END

----------------------------------------------------

存储过程 游标

过程片段:
declare master_cursor CURSOR FOR
select hangbiaoshi  from tab_Test  order  by hangbiaoshi desc
open master_cursor

FETCH NEXT FROM master_cursor into @hangbiaoshi
while @@fetch_status = 0

BEGIN
 set @tmpcontent = ''
 declare senc_cursor CURSOR FOR
 select userName,content,date from tab_information where guanlianbiaoshi = @hangbiaoshi
 open senc_cursor
 FETCH NEXT FROM senc_cursor  into @user,@content,@date
 while @@fetch_status = 0
 begin

 set @tmpcontent =  @tmpcontent + @user + ':' + @content + '(' + convert(varchar(50),@date,120) + ')' + '<br>'
 FETCH NEXT FROM senc_cursor  into @user,@content,@date
 end
 
 insert into #resultTable values (@hangbiaoshi,@tmpcontent)

 CLOSE senc_cursor
 DEALLOCATE senc_cursor
 
FETCH NEXT FROM master_cursor into @hangbiaoshi
END

CLOSE master_cursor
DEALLOCATE master_cursor

select * from #resultTable

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!