详细内容
利用游标执行循环操作
发布日期:2010-10-08     点击:2943     字体:[ ]

    该例子演示利用游标查询一个数据表,如果这个数据表里的每一条数据在另一张表里已有记录,则不新增数据,如果该条数据在另一张表不存在,则新增一笔。


--定义变量用来存储ID
declare @pOID nvarchar(40)
declare @pPID nvarchar(40)
set @pPID='DA05F597-8CFE-416B-9F6C-C2467713B0F3'
--定义游标
declare pro_Insert cursor scroll for

select OID from TblProjectMain 

--打开游标
open pro_Insert
fetch next from pro_Insert into @pOID

--执行循环体
while @@fetch_status =0
begin

if not exists(select OID from TblProjectJLPerson where PersonOID=@pPID and ProjectOID=@pOID)
begin
insert into TblProjectJLPerson(OID,ProjectOID,PersonOID) values(newid(),@pOID,@pPID)
end


fetch next from pro_Insert into @pOID

end

--关闭游标
close pro_Insert
deallocate pro_Insert
go

 

用户评论
昵称 
内容  *
验证码   
   
Copyright © 2010 zdbase.com All Rights Reserved. 苏ICP备15039389号 可人软件设计