Sqlserver儲存過程生成日期維度

2021-09-07 04:26:45 字數 4100 閱讀 8308

話不多說,之前已經有一篇日誌是利用oracle的儲存過程生成日期維度表,接下來我們就用sqlserver來實現這個操作,如下面的步驟所示

1:建立日期維度表(dim_time)

use [dw]

go/****** object: table [dbo].[dim_time] script date: 12/19/2015 15:29:26 ******/

set ansi_nulls on

goset quoted_identifier on

gocreate table [dbo].[dim_time](

[the_date] [int] not null,

[date_name] [nvarchar](30) null,

[the_year] [int] null,

[year_name] [nvarchar](30) null,

[the_quarter] [int] null,

[quarter_name] [nvarchar](30) null,

[the_month] [int] null,

[month_name] [nvarchar](30) null,

[the_week] [int] null,

[week_name] [nvarchar](30) null,

[week_day] [int] null,

[week_day_name] [nvarchar](30) null,

constraint [pk_dim_time] primary key clustered

([the_date] asc

)with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary]

) on [primary]go

2:建立生成日期維表資料的儲存過程

use [dw]

go/****** object: storedprocedure [dbo].[sp_create_time_dimension] script date: 12/20/2015 12:51:17 ******/

set ansi_nulls on

goset quoted_identifier on

goalter procedure [dbo].[sp_create_time_dimension]

@begin_date nvarchar(50)='2014-07-01' ,

@end_date nvarchar(50)='2015-12-31'

as/*

sp_create_time_dimension: 生成時間維資料

begin_date: 起始時間

end_date:結束時間

*/declare

@ddate date=convert(date,@begin_date),

@v_the_date varchar(10),

@v_the_year varchar(4),

@v_the_quarter varchar(2),

@v_the_month varchar(10),

@v_the_month2 varchar(2),

@v_the_week varchar(2),

@v_the_day varchar(10),

@v_the_day2 varchar(2),

@v_week_day nvarchar(10),

@adddays int=1;

while (@ddate<=convert(date,@end_date))

begin

set @v_the_date=convert(char(10),@ddate,112);--key值

set @v_the_year=datepart("yyyy",@ddate);--年

set @v_the_quarter=datepart("qq",@ddate);--季度"

set @v_the_month=datepart("mm",@ddate);--月份(字元型)

--set @v_the_month2=to_number(to_char(ddate, 'mm'));--月份(數字型)

set @v_the_day=datepart("dd",@ddate);--日(字元型)

--set @v_the_day2=to_char(ddate, 'dd');

set @v_the_week=datepart("ww",@ddate);--年的第幾周

set @v_week_day=datepart("dw",@ddate); --星期幾

insert into dim_time(the_date,date_name,the_year,year_name,the_quarter,quarter_name,the_month,month_name,the_week,week_name,week_day,week_day_name)

values(

@v_the_date,

convert(nvarchar(10),@v_the_year)+'年'+convert(nvarchar(10),@v_the_month)+'月'+convert(nvarchar(10),@v_the_day)+'日',

@v_the_year,

convert(nvarchar(10),@v_the_year)+'年',

@v_the_quarter,

convert(nvarchar(10),@v_the_year)+'年'+convert(nvarchar(10),@v_the_quarter)+'季度',

case when @v_the_month>=10 then

convert(int,(convert(nvarchar(10),@v_the_year)+convert(nvarchar(10),@v_the_month)))

else convert(int,convert(nvarchar(10),@v_the_year)+'0'+convert(nvarchar(10),@v_the_month)) end,

convert(nvarchar(10),@v_the_year)+'年'+convert(nvarchar(10),@v_the_month)+'月',

@v_the_week

,'第'+convert(nvarchar(10),@v_the_week)+'周',

@v_week_day,

case @v_week_day-1

when 1 then '星期一'

when 2 then '星期二'

when 3 then '星期三'

when 4 then '星期四'

when 5 then '星期五'

when 6 then '星期六'

when 0 then '星期日'

else '' end

);set @ddate=dateadd(day,@adddays,@ddate);

continue

if @ddate=dateadd(day,-1,convert(date,@end_date))

break

end

3:執行儲存過程

use [dw]

godeclare @return_value

intexec @return_value =[dbo].[sp_create_time_dimension]

select

'return value

' =@return_value

go

4:檢視2023年12月份的維度表內容如下圖所示,大功告成

5:注意和擴充套件

a:可以根據思路運用到各種資料庫當中,例如本文就是根據oracle的procedure改造過來的

b:注意上表中的最後乙個欄位week_day星期幾的字段,星期一 至 星期天依次為 2,3,4,5,6,7,1

SQL Server 儲存過程生成insert語句

原文 sql server 儲存過程生成insert語句 你肯定有過這樣的煩惱,同樣的表,不同的資料庫,加入你不能執行select insert 那麼你肯定需要一條這樣的儲存過程,之需要傳入表明,就會給你生成資料的插入語句。當然資料表數量太大,你將最好用別的方式 create proc dbo sp...

SQL Server 儲存過程生成insert語句

你肯定有過這樣的煩惱,同樣的表,不同的資料庫,加入你不能執行select insert 那麼你肯定需要一條這樣的儲存過程,之需要傳入表明,就會給你生成資料的插入語句。當然資料表數量太大,你將最好用別的方式 create proc dbo spgeninsertsql tablename varcha...

sql server儲存過程

建立表的語句 create table student sno int primary key,sname nvarchar 30 sgentle nvarchar 2 sage int,sbirth smalldatetime,sdept nvarchar 30 drop table studen...