通过游标循环插入数据

--从其他表中取得两个字段,插入到另外一个表中  

declare @ObjectID numeric(18,0)
  declare @UserID numeric(18,0)
  DECLARE Param_Cursor CURSOR FOR 
select a.objectid,b.UserID from std_ObjectInfo a, std_UserInfo b,std_Activetracks c where a.HoldID in(
      select holdid from std_Objhold where HoldTreePath like '%126652%') 
      and a.HoldID=b.HoldID and a.ObjectID = c.ObjectID and c.TransType = 1 and a.isDeleted=0
OPEN Param_Cursor
FETCH NEXT FROM Param_Cursor INTO @ObjectID,@UserID
WHILE @@FETCH_STATUS = 0
BEGIN
insert into vt_std_downinfocache(ObjectID, SendSource, Transtype,[Content], SendContent, SendUserID, DownInfoType, SubmitTime, SendFlag, Remark, CMDTypeID) 
values(@ObjectID,153,0,'DD,1','DD,1',@UserID,1502,getdate(),0,'断电报警开关指令',30000)

FETCH NEXT FROM Param_Cursor INTO @ObjectID,@UserID
end
CLOSE Param_Cursor
DEALLOCATE Param_Cursor

 
select top 1500* from vt_std_downinfocache where SendContent = 'DD,1' order by SubmitTime desc
update std_downinfocache set SendFlag=-1,SubmitTime=GETDATE() where SendFlag = 0   --SendFlag设置成-1才生效

你可能感兴趣的:(database)