我正在尝试获取一系列日期的工作日数并将其分配给变量,我似乎无法得到它。我已经尝试了我的子查询,它可以很好地为我提供不同的日期,但我需要计算它们。这是我希望的代码:
declare @end date='2016/05/06'
declare @begin date = DATEADD(month, DATEDIFF(month, 0, @end), 0)
declare @begin31 date = DATEADD(MONTH, -1, @end)
declare @tmprocdays int
@tmprocdays = select count(*) from (select distinct WORKDATE from Daily where WORKDATE between @begin and @end) <<<----- NOT WORKING
错误说问题就在附近了。我试过把括号中的东西放在括号中,但也没有用。
答案 0 :(得分:1)
试试这个
declare @end date='2016/05/06'
declare @begin date = DATEADD(month, DATEDIFF(month, 0, @end), 0)
declare @begin31 date = DATEADD(MONTH, -1, @end)
declare @tmprocdays int
set @tmprocdays = select count(*) from (select distinct WORKDATE from Daily where WORKDATE between @begin and @end) a <<<----- NOT WORKING
或将上面的最后两行替换为:
select @tmprocdays = count(*)
from
(
select distinct WORKDATE from Daily where WORKDATE between @begin and @end
) a
或甚至更好
select @tmprocdays = count(distinct WORKDATE) from Daily where WORKDATE between @begin and @end
答案 1 :(得分:1)
主要问题是子查询周围缺少括号。
以下是代码的一些修复:
declare @end date = '2016-05-06';
declare @begin date = DATEADD(month, DATEDIFF(month, 0, @end), 0);
declare @begin31 date = DATEADD(MONTH, -1, @end);
declare @tmprocdays int;
select @tmprocdays = count(distinct WORKDATE)
from Daily
where WORKDATE between @begin and @end ;
的变化:
@tmprocdays =
放在select
语句中。您的子查询缺少括号。count(distinct)
。最后三个是化妆品。
答案 2 :(得分:1)
from子句必须始终包含表名,或者在子查询的情况下必须包含别名:
如果在子查询之后添加任何标识符(字母单词),它将正常工作,例如:
@tmprocdays = select count(*) from (select distinct WORKDATE from Daily where WORKDATE between @begin and @end) xxx