编者注:小甲问:达人们的经验是从哪里来的?答曰:经验来源于实践,来源于生活中的点滴注意。学会总结,学会注意,学会发现...你也能浴火重生,跳脱升华...跟随笔者,我们知道了如何筛选数据列的信息,知道了分割字符串的简单函数,还知道了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
原文链接:http://www.cnblogs.com/tylerdonet/archive/2010/12/17/1909502.html