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
来源:https://www.cnblogs.com/blsong/archive/2009/12/07/1618519.html