我想像年份一样拆分日期部分并将其分配给存储过程中的变量。我在 sql azure 中运行该存储过程。它会抛出错误“此版本的 SQL Server 不支持对 'MASTER..spt_values' 中的数据库和/或服务器名称的引用。 ”
代码:
declare @Year int
SET @Year =DATEPART(YYYY,GETDATE())
create table #SundayDates (Sunday datetime,NextSunday datetime)
INSERT INTO #SundayDates(Sunday,NextSunday)
SELECT max(dates),MAX(DATEADD(DD,+7,dates)) AS last_sunday from
(
SELECT dateadd(day,number-1,DATEADD(year,@year-1900,0)) AS dates
FROM MASTER..spt_values WHERE type='p' and
number between 1 and DATEDIFF(day,DATEADD(year,@year-1900,0),DATEADD(year,@year-1900+1,0))
) as t
WHERE DATENAME(weekday,dates)='sunday' GROUP BY DATEADD(month,datediff(month,0,dates),0)