这个要建立一个作业啊
作业运行时间放在月初,sql脚本如下所示:
---sqlserverdeclare @name varchar(10)
set @name = convert(varchar(2),year(getdate()))+convert(varchar(2),month(getdate()))
if not exists(select null from sysobjects where xtype='u' and name =@name )
begin
---建表
exec
('
create table '+@name+' (
列名 ...
)
')
end
go
试一试,如有疑问,及时沟通!
今天是X号
a = x % 7, 求余
b = floor(X / 7) , x/7 然后取整
c = 本月1号是周几, 计算从1号 到a号是否包含周二
如果包含,结果为b+1
否则,结果为b
最终的SQL语句为:
SELECT
if(dayofmonth(now()) % 7=0, dayofmonth(now())/7,
if((1 between weekday(date_sub(now(), interval dayofmonth(now())-1 day)) and weekday(date_sub(now(), interval dayofmonth(now())+dayofmonth(now())%7-1 day))) or
(8 between weekday(date_sub(now(), interval dayofmonth(now())-1 day)) and weekday(date_sub(now(), interval dayofmonth(now())+dayofmonth(now())%7-1 day))), floor(dayofmonth(now())/7)+1, floor(dayofmonth(now())/7))
)
如果使用变量改写一下SQL,看起来更简单一些:
set @a=dayofmonth(now()) % 7
set @b=dayofmonth(now()) / 7
set @c=weekday(date_sub(now(), interval dayofmonth(now())-1 day))
set @d=weekday(date_sub(now(), interval dayofmonth(now())+@a-1 day))
SELECT
if(@a=0, @b,
if((1 between @c and @d) or (8 between @c and @d), floor(@b)+1, floor(@b))
) as result
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)