mssql游标的简单应用

--游标修改数据
declare alarm_cursor cursor global SCROLL for
select lsc_id,station_id,alarm_time,clear_time,start_time,end_time from #tmpTelemetertwoday  
open alarm_cursor 

--声明变量
declare @lsc_id int
declare @station_id int
declare @alarm_time datetime 
declare @clear_time datetime 
declare @start_time datetime 
declare @end_time datetime 
--取下一条数据源
 fetch next from alarm_cursor into @lsc_id,@station_id,@alarm_time,@clear_time,@start_time,@end_time

----循环@@Fetch_STATUS=0
while @@Fetch_STATUS=0
begin
	--停电开始时间早于查询时间,全部等于查询开始时间
	  if(@alarm_time<@ts )
	   begin
	   set @alarm_time=@ts 
	   end
	--停电结束时间晚查询时间,全部等于查询结束时间
	  if(@clear_time>@te )
	   begin
	   set @clear_time=@te
	   end


	--油机开始时间早于查询时间,全部等于查询开始时间
	  if(@start_time<@ts )
	   begin
	   set @start_time=@ts 
	   end
	--油机结束时间晚查询时间,全部等于查询结束时间
	  if(@end_time>@te )
	   begin
	   set @end_time=@te
	   end
   --把符合条件的数据插入到另一张临时表
	insert into #tmpTelemetertwodaytwo   (lsc_id,station_id,alarm_time,clear_time,start_time,end_time) values(@lsc_id,@station_id,@alarm_time,@clear_time,@start_time,@end_time )
--取下一条数据源
fetch next from alarm_cursor into @lsc_id,@station_id,@alarm_time,@clear_time,@start_time,@end_time
end
--关闭游标
close alarm_cursor
deallocate alarm_cursor


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值