我需要在表A上编写一个插入,更新触发器,该操作将从表B的所有行中删除所有行,该表的某一列(例如Desc)的值类似于在表A的列中插入/更新的值(例如Col1)。我将如何编写它,以便同时处理Update和Insert案例。我将如何确定是否为更新或插入执行了触发器。
3 回答
幕布斯7119047
TA贡献1794条经验 获得超8个赞
CREATE TRIGGER dbo.TableName_IUD
ON dbo.TableName
AFTER INSERT, UPDATE, DELETE
AS
BEGIN
SET NOCOUNT ON;
--
-- Check if this is an INSERT, UPDATE or DELETE Action.
--
DECLARE @action as char(1);
SET @action = 'I'; -- Set Action to Insert by default.
IF EXISTS(SELECT * FROM DELETED)
BEGIN
SET @action =
CASE
WHEN EXISTS(SELECT * FROM INSERTED) THEN 'U' -- Set Action to Updated.
ELSE 'D' -- Set Action to Deleted.
END
END
ELSE
IF NOT EXISTS(SELECT * FROM INSERTED) RETURN; -- Nothing updated or inserted.
...
END
- 3 回答
- 0 关注
- 1260 浏览
添加回答
举报
0/150
提交
取消