如果给定参数没有这样的记录,我试图返回默认值。这是sql查询:
DECLARE @List VARCHAR(max)
DECLARE @date VARCHAR(max)
SET @List = '''4401366'',''3115880'''
SET @date = '''2018-02-09 00:00:00.000'''
DECLARE @SQL NVARCHAR(max)
SET @SQL = N'
select EmpNo, attendanceStatus =
CASE
WHEN AttDate is null then ''false''
WHEN max(AttTimeIn) is null and max(AttTimeOut) is null THEN ''false''
WHEN max(AttTimeIn) is not null and max(AttTimeOut) is not null then(case when (max(AttTimeIn) < max(AttTimeOut)) then ''false'' else ''true'' end)
WHEN max(AttTimeIn) is not null and max(AttTimeOut) is null THEN ''true''
else ''false''
end
from tablename
where empNo in (' +@list+ ') AND AttDate = ('+ @date +')
group by EmpNo, AttDate'
EXEC sp_executesql @SQL
更具体地说,如果给定日期(对于每个员工)没有这样的记录,我希望attendanceStatus返回false。我怎样才能做到这一点?
答案 0 :(得分:1)
我相信你需要这样的东西,但没有数据就很难测试:
SET @SQL = N'
SELECT T0.EmpNo
, attendanceStatus = CASE
WHEN T1.empNo is null
THEN ''false''
WHEN max(T1.AttTimeIn) is null and max(T1.AttTimeOut) is null
THEN ''false''
WHEN max(T1.AttTimeIn) is not null and max(T1.AttTimeOut) is not null
THEN(CASE WHEN (max(T1.AttTimeIn) < max(T1.AttTimeOut))
THEN ''false'' else ''true'' END)
WHEN max(T1.AttTimeIn) is not null and max(T1.AttTimeOut) is null
THEN ''true''
ELSE ''false''
END
FROM tablename T0
LEFT JOIN tablename T1
ON T1.empNo = T0.empNo
AND T1.AttDate = ('+ @date +')
WHERE T0.empNo in (' +@list+ ')
GROUP BY T0.EmpNo
, T1.AttDate
'
答案 1 :(得分:0)
你可以试试这个。
DECLARE @List VARCHAR(max)
DECLARE @date VARCHAR(max)
SET @List = '''4401366'',''3115880'''
SET @date = '''2018-02-09 00:00:00.000'''
DECLARE @SQL NVARCHAR(max)
SET @SQL = N'
select EmpNo, attendanceStatus =
CASE WHEN AttDate = ('+ @date +') THEN
CASE
WHEN max(AttTimeIn) is null and max(AttTimeOut) is null THEN ''false''
WHEN max(AttTimeIn) is not null and max(AttTimeOut) is not null then(case when (max(AttTimeIn) < max(AttTimeOut)) then ''false'' else ''true'' end)
WHEN max(AttTimeIn) is not null and max(AttTimeOut) is null THEN ''true''
ELSE ''false''
END
ELSE ''false'' END
from tablename
where empNo in (' +@list+ ')
group by EmpNo, AttDate'
EXEC sp_executesql @SQL
答案 2 :(得分:0)
这应该有效
DECLARE @List VARCHAR(max)
DECLARE @date VARCHAR(max)
SET @List = '''4401366'',''3115880'''
SET @date = '''2018-02-09 00:00:00.000'''
DECLARE @SQL NVARCHAR(max)
SET @SQL = N'
--check if employee exists
with empTable as (
SELECT DISTINCT EmpNo
FROM tablename
WHERE empNo in (' +@list+ '))
select Et.EmpNo, attendanceStatus =
CASE
WHEN T.AttDate is null then ''false''
WHEN max(T.AttTimeIn) is null and max(T.AttTimeOut) is null THEN ''false''
WHEN max(T.AttTimeIn) is not null and max(T.AttTimeOut) is not null then(case when (max(T.AttTimeIn) < max(T.AttTimeOut)) then ''false'' else ''true'' end)
WHEN max(T.AttTimeIn) is not null and max(T.AttTimeOut) is null THEN ''true''
else ''false''
end
from
empTable eT
LEFT JOIN tablename T ON
et.EmpNo=T.EmpNo
where T.AttDate = ('+ @date +')
group by et.EmpNo, T.AttDate'
EXEC sp_executesql @SQL
答案 3 :(得分:0)
我重写了您的代码,简化了案例陈述并删除了一些代码
AttDate is null then 'false'
AttDate永远不会为null,因为它包含在 where子句
中我还更改了 @date 的数据类型,并将其作为日期的参数化查询。由于 @List ,您的脚本仍然容易被sql注入。将其拆分为表可以避免这种情况。
我不确定您要用默认值替换的内容。但这是改进的脚本:
DECLARE @List VARCHAR(max)
DECLARE @date DATE
SET @List = '''4401366'',''3115880'''
SET @date = '2018-02-09'
DECLARE @SQL NVARCHAR(max)
SET @SQL = N'
SELECT coalesce(EmpNo, ''default'') EmpNo,
CASE WHEN i is not null and (i >= o or o is null) THEN ''true'' ELSE ''false'' END
FROM
(
SELECT
Empno,
max(AttTimeIn) over (partition by EmpNo) i,
max(AttTimeOut) over (partition by EmpNo)o
FROM tablename
WHERE empNo in (' +@list+ ') AND AttDate = @date
GROUP BY EmpNo, AttDate
) x
GROUP BY EmpNo'
EXEC sp_executesql @SQL, N'@date date', @date