这是查询
`select
p.occid,
p.occdate,
f.fillername,
badgeID= null,
p.mc
p.mctime,
problemcode = isnull(p.problemcode,400),
pc.problemdesc,
p.timerresolved,
p.duration,
e.employeename,
pl.durationinminutes,
pl.timein,
pl.timeout,
problemdescription = datediff(minute,occdate,timeresolved)
from problem as p
left join filler as f
on p.fillerid = f.fillerid
left join problemlog as pl
on p.occid = pl.occid
left join problemcode as pc
on p.problemcode = pc.problemcode
left join employee as e
on pl.badgeid = e.id
where f.intials = @fillerselect and
occdate between dateadd(day, datediff(day, '19000101', @startdate),
cast(@starttime as datetime2(7))) and
dateadd(day, datediff(day, '19000101', cast @endtime as datetime2(7)))
and p.problemcode in (@problemcode)
and @Duration <= datediff(minute, occdate, timeresolved)
and case when @reportparameter like 'false'
then e.employeename in (@employee)`
我的问题在案例条款中。我收到错误声明&#34;关键字&#39;在&#39;附近的语法不正确。有没有人有任何想法如何克服这个?
我应该提到@employee可以有多个选择。当@reportparameter为“False”时,那么@employee就是“无”,说明我们没有按员工过滤。当@reportparameter为&#39; true&#39;时,我们按员工过滤,@ employee可能有多个选项,也可能没有。
答案 0 :(得分:0)
我认为,你的最后两行(以及当时的情况)应该是这样的:
and ( @reportparameter like 'false' and e.employeename in (@employee) )
答案 1 :(得分:0)
当@reportparameter ='false'
时,您似乎希望有条件地检查针对@employee的e.employeename我不建议对此操作使用case语句。而只是使用OR条件。 SQL Server优化器将比WHERE子句中的CASE语句更快地处理OR(通常。)
WHERE f.intials = @fillerselect
AND occdate BETWEEN dateadd(DAY, datediff(DAY, '19000101', @startdate), cast(@starttime AS datetime2(7))) AND dateadd(DAY, datediff(DAY, '19000101', CAST @endtime AS datetime2(7)))
AND p.problemcode IN (@problemcode)
AND @Duration <= datediff(MINUTE, occdate, timeresolved)
AND ((@reportparameter = 'false'
AND e.employeename IN (@employee))
OR (@reportparameter <> 'false'
AND e.employeename IN (@somethingelse)))
您可能希望在WHERE子句中更改其他内容。我不是100%确定你正在适当地使用IN条件。如果@employee只包含一个员工,则应使用=而不是IN。我假设@ReportParameter是一个字符串变量,但使用'false'作为值是不常见的。通常你会使用BIT并检查1或0表示True与False。
答案 2 :(得分:0)
我认为您的第二个dateadd功能已完成
dateadd(day, datediff(day, '19000101', cast @endtime as datetime2(7)))
这里我只能看到2个参数,我认为第3个参数缺失,添加第3个参数可能会解决问题