SQL中游标的使用

来源:互联网 发布:英语单词朗读软件 编辑:程序博客网 时间:2024/04/28 03:35

 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