有没有办法可以检查我想插入由触发器激活的表中是否存在值?如果值确实存在,我希望不做任何事情,如果不存在,我希望它被插入。
这是我当前的触发器
ALTER TRIGGER [dbo].[Update] On [dbo].[A] AFTER UPDATE AS Declare @Id int; SELECT @Id = Issue FROM Inserted INSERT INTO dbo.[B] (id, problem) SELECT BugId, ProjectID FROM dbo.[C] WHERE BugId = @Id and (projectid = 547)
非常感谢
答案 0 :(得分:1)
您应该按照in this SO post所述进行操作。使用IF语句检查是否存在。
答案 1 :(得分:1)
inserted
可以包含多行。 left join
可以成为您测试行是否已存在的朋友:
ALTER TRIGGER [dbo].[Update]
On [dbo].[A]
AFTER UPDATE
AS
INSERT INTO dbo.[B] (id, problem)
SELECT BugId, ProjectID
FROM
dbo.[C]
inner join
inserted i
on
c.BugID = i.Issue
left join
dbo.B
on
B.ID = c.BugID
WHERE
C.projectid = 547 and B.BugID is null