declare @CausName as char(20) declare @causenameid as int declare
@Description as varchar(200) declare @Caus_Id as int
declare @sign as char(20)
create table #tb ( Caus_Id int,CausName varchar(20),causNameid int,Caus_Description varchar(200),标志 varchar(20))
--定义游标
DECLARE myCursor1 CURSOR FOR
select Caus_Id,Caus_PersonId,Caus_Description from tbObjection_Causer where Obje_Id=25
--打开游标
OPEN myCursor1
FETCH NEXT FROM myCursor1 INTO @Caus_Id,@causenameid,@Description
WHILE @@FETCH_STATUS =0
BEGIN
select @CausName=人员姓名 from zgong_xxi where id=@causenameid
insert into #tb
select Caus_Id=@Caus_Id,CausName=@CausName,causNameid=@causenameid,
Caus_Description=@Description,标志=@sign where @Caus_Id is not null
FETCH NEXT FROM myCursor1 INTO @Caus_Id,@causenameid,@Description
END
--关闭游标
CLOSE myCursor1
--删除游标引用
Deallocate myCursor1
select * from #tb
drop table #tb
本文介绍了一个使用T-SQL创建游标并结合临时表处理特定数据集的实例。该过程包括声明变量、创建临时表、定义游标、进行数据检索与插入等步骤,展示了如何利用游标逐条处理记录。
518

被折叠的 条评论
为什么被折叠?



