创建会计年度表

时间:2013-04-11 17:56:16

标签: sql-server-2008 stored-procedures

我刚刚创建了一个financial_year表内容

- Week  (tinyint 1-53)
- Start date (date)
- End date (date)
- Year (int)

我想制作一个SP,使用参数将日期插入此表:

- Year: <2013>
- Start Year date: <12/30/2012>
- End Year date: <12/28/2013>

然后它将相应于下表中产生52周:

- Week: 1
- Start date: 12/30/2012
- End date: 01/05/2013
- Year 2013

1 个答案:

答案 0 :(得分:4)

DECLARE @d DATE = '20121230';

;WITH x AS
(
  SELECT TOP (53) n = ROW_NUMBER() OVER (ORDER BY [object_id])-1
    FROM sys.all_objects 
    ORDER BY [object_id]
)
--INSERT dbo.fiscal_year([week],[start date],[end date],[year])
SELECT 
  [week] = n+1, 
  [start date] = DATEADD(WEEK, n, @d), 
  [end date] = DATEADD(DAY, n*7+6,@d), 
  [year] = 2013
FROM x
ORDER BY n;

如果您对输出感到满意,请取消注释INSERT