我有一个表和触发器,它正在进行一些计算和更新列。
我想更新输入模式字段,希望在表格中设置0和1天。 我编写了以下代码,但没有正常工作。
WITH CTE AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY Convert(datetime, el.M_Edatetime)) AS ROW,
el.LogId
FROM
Essl_logdata el
INNER JOIN
Inserted i ON el.EnrollNo = i.EnrollNo
AND el.M_Edatetime = i.M_Edatetime
WHERE
el.EnrollNo = @INS
AND Convert(Date, el.M_Edatetime) = Convert(date, @M_datetime)
)
UPDATE Essl_logdata
SET InOutMode = '0'
FROM CTE
WHERE ROW%2 <> 0 AND Essl_logdata.LogId = CTE.LogId;
WITH CTE AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY COnvert(datetime, el.M_Edatetime)) AS ROW,
el.LogId
FROM
Essl_logdata el
INNER JOIN
Inserted i ON el.EnrollNo = i.EnrollNo
AND el.M_Edatetime = i.M_Edatetime
WHERE
el.EnrollNo = @INS
AND Convert(Date, el.M_Edatetime) = Convert(date, @M_datetime)
)
UPDATE Essl_logdata
SET InOutMode = '1'
FROM CTE
WHERE ROW%2 = 0 AND Essl_logdata.LogId = CTE.LogId;
预期结果如下。
触发器中的当前查询显示结果如下。
答案 0 :(得分:0)
WITH CTE AS
(
SELECT ROW_NUMBER() OVER(PARTITION BY COnvert(date,M_Edatetime),EnrollNo ORDER BY COnvert(datetime,M_Edatetime))AS ROW,Logid,M_Edatetime,EnrollNo FROM Essl_logdata group by M_Edatetime,Logid,EnrollNo
)
Update Essl_logdata set InOutMode = '1' FROM CTE WHERE ROW%2 = 0 and Essl_logdata.LogId = CTE.LogId;
WITH CTE AS
(
SELECT ROW_NUMBER() OVER(PARTITION BY COnvert(date,M_Edatetime),EnrollNo ORDER BY COnvert(datetime,M_Edatetime))AS ROW,Logid,M_Edatetime,EnrollNo FROM Essl_logdata group by M_Edatetime,Logid,EnrollNo
)
Update Essl_logdata set InOutMode = '0' FROM CTE WHERE ROW%2 <> 0 and Essl_logdata.LogId = CTE.LogId;
我找到了解决方法