我有以下触发器:
BEGIN
DECLARE @email varchar(200)
DECLARE @jobcode int
DECLARE @status char(1)
DECLARE @emaild varchar(200)
DECLARE @jobcoded int
DECLARE @statusd char(1)
SET @statusd = NULL
SELECT @status = z.status, @email = p.EMail, @jobcode = z.jobID
FROM zipoutfiles z
INNER JOIN inserted AS i ON z.jobID = i.jobID
INNER JOIN PS_LoginUser AS p ON z.UserID = p.UserID
SELECT @statusd = z.status, @emaild = p.EMail, @jobcoded = z.jobID
FROM zipoutfiles z
INNER JOIN deleted AS d ON z.jobID = d.jobID
INNER JOIN PS_LoginUser AS p ON z.UserID = p.UserID
WHERE d.jobID = @jobcode
IF ((@status = 'D' AND @statusd = 'R') OR (@status = 'D' AND @statusd = 'E'))
BEGIN
EXEC SendMail @email, @jobcode
END
END
我希望能够在状态从E到D或R到D,但不是D到D(如果它再次更新)以及当它作为D插入时运行SendMail。我在这里做错了什么:
答案 0 :(得分:1)
不确定您的表架构是什么,但这可能会为您提供所有相应的电子邮件:
select p.EMail as Email, z.JobId as JobCode
from ZipOutFiles as ZOF inner join
inserted as i on i.JobId = ZOF.JobId inner join
PS_LoginUser as PLU on PLU.UserId = ZOF.UserId left outer join
deleted as d on d.JobId = ZOF.JobId
where
( d.Status = 'E' and i.Status = 'D' ) or -- E -> D.
( d.Status = 'R' and i.Status = 'D' ) or -- R -> D.
( d.Status is NULL and i.Status = 'D' ) -- Inserted D.
我认为你并没有真正更新JobId
。如果是这样,你如何匹配前后行?
还假设Status
不能是NULL
。如果是这样,则需要修改最后一个条件以正确检测deleted
表中没有找到相应的行。