編者註:小甲問:達人們的經驗是從哪裡來的?答曰:經驗來源於實踐,來源於生活中的點滴注意。學會總結,學會注意,學會發現...你也能浴火重生,跳脫升華...跟隨筆者,我們知道了如何篩選資料列的資訊,知道了分割字串的簡單函數,還知道了SET QUOTED_IDENTIFIER OFF語句的作用。現在我們要繼續跟隨筆者,分享他的SQL的點點滴滴,現在開始....
資料倉儲中有時間表,儲存時間資訊,這個預存程序接收開始時間結束時間,寫入時間具體資訊。有高手用excel函數功能很快能產生INSERT語句不會啊,只能用這個。
參考知識
- vSET ANSI_NULLS ON
- GO
-
- SET QUOTED_IDENTIFIER ON
- GO
-
- /*-------------------------------------------------------- 。
- *generate Date demention
- *@startdate '01/01/2010'
- *@enddate '12/30/2010'
- ---------------------------------------------------------*/
-
- CREATE PROCEDURE dbo.AddDateTime
- @startdate datetime,
- @enddate datetime
- AS
- declare @i int
- set @i=1
-
- while(DATEDIFF(DD,@startdate,@enddate)>=0)
- begin
-
- insert into [ReportServer].[dbo].[D_DATE](
- [DATE_KEY], --primarykey
- [DATE], --datetime
- [FULL_DATE_DESCRIPTION], --detail date
- [DAY_OF_WEEK], --day of week from 1 to 6
- [CALENDAR_MONTH], --month of year from 1 to 12
- [CALENDAR_YEAR], --year
- [FISCAL_YEAR_MONTH], --fiscal year
- [HOLIDAY_INDICATOR], --holiday iden
- [WEEKDAY_INDICATOR])
- select
- @i,
- CONVERT(varchar(10),@startdate,101),
- DATENAME(mm,@startdate)+' '+DATENAME(DD,@startdate)+', '+DATENAME(YYYY,@startdate),
- DATEPART(DW,@startdate),
- DATEPART(MM,@startdate),
- DATEPART(YYYY,@startdate),
- 'F'+ CONVERT(varchar(7),@startdate,23),
- case
- when( CONVERT(varchar(5),@startdate,101) in ('01/01','01/05','02/05','03/05','01/10','02/10','03/10','04/10','05/10','06/10') ) then 1
- else 0 end,
- case
- when(DATENAME(DW,@startdate) in ('Saturday','Sunday')) then 1
- else 0 end
-
- if DATEDIFF(DD,@startdate,@enddate)>=0
-
- begin
- set @startdate = DATEADD(dd,1,@startdate)
- set @i=@i+1
- continue
- end
-
- else
-
- begin
- break
- end
-
- end
-