SqlServer 游标

DECLARE @PointID nvarchar(100),@RowID1 nvarchar(100)
 
--声明一个游标( 可以把游标想象成每一条记录 )用来遍历查询到的结果
DECLARE
    C_paraId CURSOR FOR 
      SELECT 
          a.PointID 
        FROM Report_Data a
        LEFT JOIN Report_Row b ON a.RowID = b.RowID
        LEFT JOIN Report_Day c ON b.ReportID = c.ReportID
        LEFT JOIN Report_Point d ON a.PointID = d.PointID
        LEFT JOIN Report_Monitor e ON d.MonitorID = e.MonitorID
        WHERE (1=1) AND (2=2)   and e.MonitorType=6 and c.MonitorId='5139cb6b-e34a-4d92-a2f8-1924f1ff7760'    and  a.PointID=418
        order by e.MonitorOrder,d.PointOrder,b.RowHour 

--打开游标
OPEN C_paraId 
--获取游标指向的数据
FETCH NEXT
FROM  C_paraId INTO @PointID
--使用游标遍历集合( @@FETCH_STATUS是默认全局变量,不用声明, =0表示匹配到了记录 )
WHILE
    @@FETCH_STATUS = 0 
        BEGIN
   update [DB_PEMS].[dbo].[Report_Data]  set PointID='454'where PointID=@PointID   
     --游标指向下一条数据
     FETCH NEXT
     FROM C_paraId INTO @PointID
END 
--关闭游标
CLOSE C_paraId 
--释放游标
DEALLOCATE C_paraId

  

posted @ 2021-07-29 08:58  King、Wang  阅读(42)  评论(0编辑  收藏  举报