sql server 的遊標
-- sql server 中的遊標
--聲明遊標
/*
declare cursorname [insensitive] [scroll] cursor
for <select-查詢塊>
[for {read only|update[of<列名>[,...,n]]}]
Insensitive 表示把取出來的數據存入一個在tempdb庫中創建的臨時表,任何通過這個遊標進行的操作,都會在這個臨時表裏進行。所有對基本表的改動都不會在用遊標進行的操作中體現出來,不用該關鍵字,則用戶對基本表所進行的任何操作都將在遊標中得到體現。
Scroll: 指定所有的提取選項(first,last,prior,wext,relative,absolute)均可用,允許刪除和更新(假定沒有使用insensitive選項)
for read only :遊標只讀。不允許通過只讀遊標進行數據的更新。
for update[of <列名>[,...,n]]:遊標是可修改的。定義在這個遊標裏可以更新的列。如果定義了[of<列名>[,...n]],則只有其中的列可以被修改;否則,遊標裏的所有列都可以被修改。
*/
declare @tagname nvarchar(max)
declare @max_datetime datetime
declare mycursor cursor
for
select a.tagname,max(isnull(b.DateTime,0))
from tblfminfolive a
left join live_dblink.gb_scada_live.dbo.LiveData b on a.tagname=b.tagname and b.provider=‘TAX‘
group by a.tagname;
-- 打開遊標
open mycursor;
-- 提取數據
-- fetch[[next|prior|first|last|absoute n|relative n] from] <遊標名> [into @變量名[,...,n]]
fetch next from mycursor into @tagname,@max_datetime;
/*全局變量@@fetch_status表示fetch語句的狀態
0 表示提取正常
1 表示已經取到了數據集的末尾
其他值均表明操作出了問題
*/
-- 進入循環
while (@@fetch_status=0)
BEGIN
IF right(@tagname,2) !=‘YL‘
-- 寫入flux,PlusTotalFlux,ReverseTotalFlux to 目標表
insert into live_dblink.gb_scada_live.dbo.LiveData(
provider,sitename,tagname,MeterName,DateTime,value)
select ‘TAX‘,b.username,b.tagname,b.MeterName,a.readtime
,case right(@tagname,2)
when ‘SL‘ then a.flux
when ‘ZL‘ then a.plustotalflux
when ‘FL‘ then a.reversetotalflux
end
FROM tblfmreaddata a ,tblfminfolive b
WHERE a.fmaddress+right(@tagname,3)=@tagname and b.tagname=@tagname
AND a.readtime>@max_datetime
ELSE
--寫入 pressure to 目標表
insert into live_dblink.gb_scada_live.dbo.LiveData(
provider,sitename,tagname,MeterName,DateTime,value)
select ‘TAX‘,b.username,b.tagname,b.MeterName,a.readtime,isnull(a.pressure,0)
FROM tblfmreaddatapress a,tblfminfolive b
WHERE a.fmaddress+right(@tagname,3)=@tagname and b.tagname=@tagname
AND a.readtime>@max_datetime
-- 獲取下一條數據
fetch next from mycusor into @tagname,@max_datetime;
END;
-- 關閉遊標
close mycursor;
-- 釋放遊標
deallocate mycursor;
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 由於sql server 的未知原因,cursor未能成功執行,於是創建臨時表,原理類似cursor,如下:
--------------------------------------------------------------------------------
declare @cun int
declare @maxcun int
declare @max_datetime datetime
declare @fmaddress varchar(40)
create table #tmp_fmaddress
(
pid int identity(1,1),
fmaddress varchar(100),
max_datetime datetime
)
insert into #tmp_fmaddress(fmaddress,max_datetime)
select a.fmaddress,max(isnull(b.DateTime,0))
from tblfminfolive a
left join live_dblink.gb_scada_live.dbo.LiveData b on a.tagname=b.tagname and b.provider=‘TAX‘
and b.DateTime>dateadd(day,-2,getdate())
group by a.fmaddress
set @cun=1
select @maxcun=max(pid) from #tmp_fmaddress
-- 進入循環
while @cun<@maxcun+1
BEGIN
select @fmaddress=fmaddress,@max_datetime=max_datetime from #tmp_fmaddress where pid=@cun
------流量數據
select fmaddress,readtime,flux,PlusTotalFlux,ReverseTotalFlux
into #temp_flux
FROM tblfmreaddata WHERE fmaddress=@fmaddress
AND readtime>@max_datetime
AND readtime>dateadd(day,-2,getdate())
-----壓力數據
select fmaddress,readtime, isnull(pressure,0) press
into #temp_press
FROM tblfmreaddatapress WHERE fmaddress=@fmaddress
AND readtime>@max_datetime
and readtime>dateadd(day,-2,getdate())
-- 寫入flux,PlusTotalFlux,ReverseTotalFlux to 目標表
insert into live_dblink.gb_scada_live.dbo.LiveData(
provider,sitename,tagname,MeterName,DateTime,value)
select ‘TAX‘,b.username,b.tagname,b.MeterName,a.readtime
,case right(b.tagname,2)
when ‘SL‘ then a.flux
when ‘ZL‘ then a.plustotalflux
when ‘FL‘ then a.reversetotalflux
end
FROM #temp_flux a,tblfminfolive b
where a.fmaddress=b.fmaddress and b.tagname!=@fmaddress+‘_YL‘
-- 寫入 pressure
insert into live_dblink.gb_scada_live.dbo.LiveData(
provider,sitename,tagname,MeterName,DateTime,value)
select ‘TAX‘,b.username,b.tagname,b.MeterName,a.readtime,a.press
FROM #temp_press a,tblfminfolive b
where a.fmaddress=b.fmaddress and b.tagname=@fmaddress+‘_YL‘
drop table #temp_flux
drop table #temp_press
-- 獲取下一條數據
set @cun=@cun+1
END
sql server 的遊標