--创建一个游标
declare youbiao cursor for --my_cursor为游标的名称,随便起
select ID from vw_OA_dayworkinfo_basic where Statue=1 --这是游标my_cursor的值,这里随便发挥看业务场景
--打开游标
open youbiao --没什么好说的
--变量
declare @id int --声明变量 ‘declare’为声明变量 ‘@name’为变量名称 后面为变量类型
--循环游标
fetch next from youbiao into @id --获取my_cursor的下一条数据,其中为两个字段分别赋值给@id,@name
while @@FETCH_STATUS=0 --假如检索到了数据继续执行
begin
insert into OA_workinfo(Sysid, DayWorkTypeId, DetialCont, Flag, CreateUserId, CreateDate, ModelName, FunctionName, Statue, LinkRequest)
select Sysid, DayWorkTypeId, DetialCont, Flag, CreateUserId, CreateDate, ModelName, FunctionName, Statue, LinkRequest from OA_dayworkinfo where ID = @id
update OA_dayworkinfo set mainID = @@IDENTITY where ID=@id
fetch next from youbiao into @id --获取下一条数据并赋值给变量
end--关闭释放游标
close youbiao
deallocate youbiao