熟悉SQL脚本的朋友来看一个问题,如何生成这样一个临时表?环境SQL Server 2K8
发布网友
发布时间:2022-04-07 23:14
我来回答
共2个回答
懂视网
时间:2022-04-08 03:35
master.dbo.spt_values
标签:
热心网友
时间:2022-04-08 00:43
create PROCEDURE [dbo].[weekly]
( @year varchar(4)
,@statweek int
,@endweek int
)
as
declare @max_datetime datetime
select @max_datetime=max(a.shijian)
from (
select datepart(week,DATEADD(DAY,number,@year+'0101')) as weekly,DATEADD(DAY,number,@year+'0101') as shijian
from master.dbo.spt_values
where type='p' and number<=7 ) as a
where a.weekly=1
select right(convert(char(8),(dateadd(day,number*7,@max_datetime)-6),112),4)+'~'+right(convert(char(8),dateadd(day,number*7,@max_datetime),112),4) as [周范围]
from master.dbo.spt_values
where type='p' and number<=54
and datepart(week,dateadd(day,number*7,@max_datetime)) between @statweek and @endweek
/**
执行下面语句就是你想要的结果
exec [weekly] '2012',32,34
**/