儲存過程計算報表

2021-04-19 18:08:56 字數 4975 閱讀 4795

create procedure usp_t_scdd_othermine 

-- @ipagesize int,@icurpage int,

@iyear int,@sdwid varchar(255)

--with encryption

as begin

--建立臨時表

create table #t1

(fno int identity not null,      /*序號*/ 

fdwid int null,        /*單位id*/

fdwcode varchar(255) null,      /*單位編碼*/

fdwname varchar(255) null,      /*單位名稱*/

fnum numeric(14,2),       /*總量*/

ftype varchar(255) null,      /*型別:計畫和實際*/

fyear int null,        /*年份*/

f1 numeric(14,2),

f2 numeric(14,2),   

f3 numeric(14,2),   

f4 numeric(14,2),   

f5 numeric(14,2),   

f6 numeric(14,2),   

f7 numeric(14,2),   

f8 numeric(14,2),   

f9 numeric(14,2),   

f10 numeric(14,2),   

f11 numeric(14,2),   

f12 numeric(14,2),  

)--插入單位id,單位名稱和領導列資料

if(ltrim(rtrim(@sdwid))='') set @sdwid='%'  

/*插入計畫初始資料*/

insert into #t1 (fdwid,fdwcode,fdwname,ftype,fyear)

select distinct t2.fid,t2.fcode fdwcode,t2.fname fdwname,'計畫' ftypename,t1.fyear

from t_scdd_othermine t1

left join t_sys_dw t2 on t1.fdwid=t2.fid

where t1.fyear=@iyear and t1.fdwid like @sdwid

order by t2.fcode

/*插入實際初始資料*/

insert into #t1 (fdwid,fdwcode,fdwname,ftype,fyear)

select distinct t2.fid,t2.fcode fdwcode,t2.fname fdwname,'實際' ftypename,t1.fyear

from t_scdd_othermine t1

left join t_sys_dw t2 on t1.fdwid=t2.fid

where t1.fyear=@iyear and t1.fdwid like @sdwid

order by t2.fcode

--插入其他列資料

declare @ifdwid int

declare dw_cursor cursor for

select fdwid from #t1 order by fdwcode

open dw_cursor

fetch next from dw_cursor

into @ifdwid

while @@fetch_status = 0

begin

declare content_cursor cursor for

select t1.fmonth,t1.fplan,t1.fact from t_scdd_othermine t1

left join t_sys_dw t2 on t1.fdwid=t2.fid

where t1.fyear=@iyear and t1.fdwid=@ifdwid

order by fdwid

--   ,@isum int

declare @ifmonth int,@sfplan varchar(255),@sfact varchar(255)

open content_cursor

fetch next from content_cursor into @ifmonth,@sfplan,@sfact

--   set @isum=0

while @@fetch_status = 0

begin

/*更新計畫*/

if (@ifmonth=1)  update #t1 set f1=@sfplan where fdwid=@ifdwid and ftype='計畫'

if (@ifmonth=2)  update #t1 set f2=@sfplan where fdwid=@ifdwid and ftype='計畫'

if (@ifmonth=3)  update #t1 set f3=@sfplan where fdwid=@ifdwid and ftype='計畫'

if (@ifmonth=4)  update #t1 set f4=@sfplan where fdwid=@ifdwid and ftype='計畫'

if (@ifmonth=5)  update #t1 set f5=@sfplan where fdwid=@ifdwid and ftype='計畫'

if (@ifmonth=6)  update #t1 set f6=@sfplan where fdwid=@ifdwid and ftype='計畫'

if (@ifmonth=7)  update #t1 set f7=@sfplan where fdwid=@ifdwid and ftype='計畫'

if (@ifmonth=8)  update #t1 set f8=@sfplan where fdwid=@ifdwid and ftype='計畫'

if (@ifmonth=9)  update #t1 set f9=@sfplan where fdwid=@ifdwid and ftype='計畫'

if (@ifmonth=10)  update #t1 set f10=@sfplan where fdwid=@ifdwid and ftype='計畫'

if (@ifmonth=11)  update #t1 set f11=@sfplan where fdwid=@ifdwid and ftype='計畫'

if (@ifmonth=12)  update #t1 set f12=@sfplan where fdwid=@ifdwid and ftype='計畫'

/*更新實際*/

if (@ifmonth=1)  update #t1 set f1=@sfact where fdwid=@ifdwid and ftype='實際'

if (@ifmonth=2)  update #t1 set f2=@sfact where fdwid=@ifdwid and ftype='實際'

if (@ifmonth=3)  update #t1 set f3=@sfact where fdwid=@ifdwid and ftype='實際'

if (@ifmonth=4)  update #t1 set f4=@sfact where fdwid=@ifdwid and ftype='實際'

if (@ifmonth=5)  update #t1 set f5=@sfact where fdwid=@ifdwid and ftype='實際'

if (@ifmonth=6)  update #t1 set f6=@sfact where fdwid=@ifdwid and ftype='實際'

if (@ifmonth=7)  update #t1 set f7=@sfact where fdwid=@ifdwid and ftype='實際'

if (@ifmonth=8)  update #t1 set f8=@sfact where fdwid=@ifdwid and ftype='實際'

if (@ifmonth=9)  update #t1 set f9=@sfact where fdwid=@ifdwid and ftype='實際'

if (@ifmonth=10)  update #t1 set f10=@sfact where fdwid=@ifdwid and ftype='實際'

if (@ifmonth=11)  update #t1 set f11=@sfact where fdwid=@ifdwid and ftype='實際'

if (@ifmonth=12)  update #t1 set f12=@sfact where fdwid=@ifdwid and ftype='實際'

--     set @isum=@isum+1

fetch next from content_cursor into @ifmonth,@sfplan,@sfact

endclose content_cursor

deallocate content_cursor

--   update #t1 set fsum=@isum where fno=@ifno

-- get the next author.

fetch next from dw_cursor

into @ifdwid

endclose dw_cursor

deallocate dw_cursor

--分頁顯示資料

declare @ssql nvarchar(1000)

select @ssql=

'select * from #t1

order by fdwcode,ftype

'execute sp_executesql @ssql

--返回總行數

--  select count(*) from #t1

end--go

報表中如何呼叫儲存過程?

很多時候,報表設計工具在設計時所需的資料由於查詢邏輯複雜 表間關係複雜或者其他系統原因,需要使用現有資料庫中的儲存過程返回的資料來製作報表。那麼,如何在設計報表時,呼叫含引數的儲存過程呢?本文以sqlserver sales 訂單表為例,介紹實現方法。儲存工程指令碼 create proc test...

皕傑報表資料集呼叫儲存過程

皕傑報表可以以資料集的方式來操作儲存過程 第一步 建立乙個游標 create or replace package types as type bj cursor is ref cursor end 第二步 建立儲存過程 create or replace procedure bj test pro...

Birt報表儲存過程多選引數的設定

birt對儲存過程的操作是很簡單的一行語句,只需要在data set中寫上類似這樣 如下圖 本報表是存在兩個引數,乙個允許多值的輸入引數,乙個是游標型別的輸出引數。通常情況下,對於多值問題,通常步驟如下 先設定允許多選,再在script指令碼裡面通過tostring 拼接。如下圖 圖摘自一學長筆記 ...