SQL server触发器中 update insert delete 分别给写个例子被。_百度知...
发布网友
发布时间:2022-04-27 04:26
我来回答
共1个回答
热心网友
时间:2022-04-11 18:50
INSERT:
表1 (ID,NAME)
表2 (ID,NAME)
当用户插入表1数据后,表2也被插入相同的数据
CREATE TRIGGER TRI1
ON 表1
FOR INSERT
AS
BEGIN
INSERT INTO 表2 SELECT * FROM INSERTED
END
GO
DELETE:
表1 (ID,NAME)
表2 (ID,AGE,GENDER)
当用户删除表1某条ID的数据后,表2相应ID的数据也被删除
CREATE TRIGGER TRI2
ON 表1
FOR DELETE
AS
BEGIN
DECLARE @id INT
SELECT @id FROM DELETED
DELETE 表2
WHERE ID = @id
END
GO
UPDATE:
表1 (ID,NAME)
表2 (ID,NAME)
当用户更改表1 NAME列数据后,表2相应ID的数据也同时更新
CREATE TRIGGER TRI3
ON 表1
FOR UPDATE
AS
IF UPDATE(NAME)
BEGIN
UPDATE 表2
SET A.NAME = B.NAME
FROM 表2 A, INSERTED B
WHERE A.ID = B.ID
END
GO