在儲存過程中建立臨時表來重構資料

2021-08-05 23:54:51 字數 3471 閱讀 5959

在儲存過程中建立臨時表來重構資料

有下面兩張表

這種情況下查詢出來的結果按下面的形式顯示:

id name tm score

001 小小 2011-1-1 90

001 小小 2011-2-1 80

002 小強 2011-1-5 75

這種結果是通過tab_score inner jion tab_students 來實現的,導致每個時間有乙個考核分數,然後進行羅列顯示。

然而有時客戶希望得到下面的顯示結果。

id name 年份 月份 上旬分數 中旬分數 下旬分數 全月分數

也就是每個月份有乙個顯示記錄

作為乙個和使用者互動的、可分頁儲存過程一般包括下面幾個輸入引數:

開始時間、結束時間、開始取值位置,結束取值位置、搜尋條件、排序條件
下面是通過臨時表來重構資料的乙個查詢:

複製**

–引數

declare @startdate datetime=』2011-01-15 08:00:00.000』

declare @enddate datetime=』2012-10-22 08:00:00.000』

declare @startindex int=1

declare @endindex int=100

declare @conditionstr nvarchar(200)=』1<>0』

declare @orderstr nvarchar(200)=』id desc』

–臨時表

drop table #mytable

create table #mytable

( id nvarchar(50) not null,

name nvarchar(50) not null,

yr int not null,

mth int not null,

upscore int not null,

middlescore int not null,

downscore int not null,

allscore int not null,

) –獲取月初時間

set @startdate=dateadd(mm,datediff(mm,0,@startdate),0)

set @enddate=dateadd(mm,datediff(mm,0,@enddate), 0)

–向臨時表新增資料

while @startdate<@enddate

begin

insert into #mytable (id,name,yr,mth,upscore,middlescore,downscore,allscore)

select tab_students.id,

tab_students.name,

datepart(year,@startdate) as yr,

datepart(month,@startdate) as mth,

( select isnull(sum(score),0) from tab_score

where tab_score.id = tab_students.id

and tm between @startdate and dateadd(day,10,@startdate)

)as upscore,

( select isnull(sum(score),0) from tab_score

where tab_score.id = tab_students.id

and tm between dateadd(day,10,@startdate) and dateadd(day,20,@startdate)

)as middlescore,

( select isnull(sum(score),0) from tab_score

where tab_score.id = tab_students.id

and tm between dateadd(day,20,@startdate) and dateadd(mm,1,@startdate)

)as downscore,

( select isnull(sum(score),0) from tab_score

where tab_score.id = tab_students.id

and tm between @startdate and dateadd(mm,1,@startdate)

)as allscore

from tab_students

set @startdate=dateadd(mm,1,@startdate)

end

–結果查詢

declare @selectstr nvarchar(1000)

set @selectstr=』select * from #mytable where 』 + @conditionstr +』 order by 『+@orderstr

exec (@selectstr)

複製**

複製**

–結果表

if exists (select * from sys.objects where object_id = object_id(n』[dbo].[#mytable]』) and type in (n』u』))

drop table [dbo].[#mytable]

create table #mytable

( drp decimal(8,2),

tm datetime

)declare @drp decimal(8,2)

–查詢時段:8:00 …..8:00 雨量統計算結束時間的8:00-8:00

while @starttime<@endtime

begin

select @drp= isnull(sum(drp),0) from st_pptn_r

where

stcd = @stcd and

tm >dateadd(hh,-1,@starttime) and tm<=@starttime and –向前推乙個小時到當前時間

stcd+』,』+cast(tm as nvarchar) not in (select st_pptn_r_add.stcd+』,』+cast(st_pptn_r_add.tm as nvarchar) from st_pptn_r_add)

insert into #mytable(drp,tm)

values

( @drp,

@starttime--降雨結束時間

)set @starttime=dateadd(hh,1,@starttime)

end

select * from #mytable

複製**

在ORACLE儲存過程中建立臨時表

create procedure pro asstr varchar2 100 begin str create global temporary table tablename col1 varchar2 10 col2 number on mit preserve rows execute im...

在ORACLE儲存過程中建立臨時表

create procedure pro asstr varchar2 100 begin str create global temporary table tablename col1 varchar2 10 col2 number on commit preserve rows execute...

在ORACLE儲存過程中建立臨時表

在oracle儲存過程中建立臨時表 儲存過程裡不能直接使用ddl語句,所以只能使用動態sql語句來執行 on commit delete rows 說明臨時表是事務指定,每次提交後oracle將截斷表 刪除全部行 on commit preserve rows 說明臨時表是會話指定,當中斷會話時or...