以前碰到需要用游标的地方总是懒得去深究总是用临时表加循环的方式替代了,今天用到了就记录下,下面是今天使用过程中的实例带上注释,因为用到的机会不多所以留个记号备忘。

 

declare @userid varchar(10)  
declare cursor1 cursor for			   --定义游标cursor1
select userid from Base_User where IsDisabled=0    --使用游标的对象(跟据需要填入select文)
open cursor1                                       --打开游标
fetch next from cursor1 into @userid		   --将游标向下移1行,获取的数据放入之前定义的变量@userid中

while @@FETCH_STATUS=0				  --判断是否成功获取数据
begin
	insert into Lux_Work_Right(id,createby,createon,updateby,updateon,updateversion,Function_TableName,OwnerID,ViewerID)
select  convert(varchar(10),GETDATE(),112)+ convert(varchar(50),NEWID()) ,'系统管理员',CONVERT(varchar(20),GETDATE(),120),
'系统管理员',CONVERT(varchar(20),GETDATE(),120),'0','每日工作安排', @userid,'P001'

 fetch next from cursor1 into @userid               --将游标向下移1行
 end
 close cursor1					    --关闭/释放游标
 deallocate cursor1
 

  

 

posted on 2012-09-05 11:49  falcon_fei  阅读(192)  评论(0)    收藏  举报