0

我刚刚创建了一个财政年表内容

- 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
4

1 回答 1

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.

于 2013-04-11T17:59:49.867 回答