sql server 的遊標

-- sql server 中的遊標sql

--聲明遊標ide

/*fetch

declare cursorname [insensitive] [scroll] cursorspa

for <select-查詢塊>server

[for {read only|update[of<列名>[,...,n]]}]flux

Insensitive 表示把取出來的數據存入一個在tempdb庫中建立的臨時表,任何經過這個遊標進行的操做,都會在這個臨時表裏進行。全部對基本表的改動都不會在用遊標進行的操做中體現出來,不用該關鍵字,則用戶對基本表所進行的任何操做都將在遊標中獲得體現。get

Scroll: 指定全部的提取選項(first,last,prior,wext,relative,absolute)都可用,容許刪除和更新(假定沒有使用insensitive選項)it

for read only :遊標只讀。不容許經過只讀遊標進行數據的更新。io

for update[of <列名>[,...,n]]:遊標是可修改的。定義在這個遊標裏能夠更新的列。若是定義了[of<列名>[,...n]],則只有其中的列能夠被修改;不然,遊標裏的全部列均可以被修改。table

 

*/

 

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

相關文章
相關標籤/搜索