这应该可以完成这项工作。
表和视图定义。
CREATE TABLE Customer
(ID int,
FName varchar(200),
SName varchar(200),
RoleID int);
CREATE TABLE CustomerRole
(RID int,
Name varchar(100));
CREATE VIEW ViewCustomer AS
SELECT *
FROM Customer JOIN CustomerRole on RoleID = RID;
触发器定义。
CREATE TRIGGER ViewCustomerTrigger ON ViewCustomer
INSTEAD OF UPDATE
AS
BEGIN
SET NOCOUNT ON
UPDATE Customer
SET FName = I.FName,
SName = CASE I.FName WHEN 'Peter' THEN 'Smith' ELSE I.SName END,
RoleID = I.RoleID
FROM INSERTED I JOIN Customer C ON I.ID = C.ID
UPDATE CustomerRole
SET Name = I.Name
FROM INSERTED I JOIN CustomerRole R ON I.RID = R.RID
END
GO
样本数据
INSERT INTO Customer (ID, FName, SName, RoleID)
VALUES (1, 'John', 'Wayne', 1);
INSERT INTO Customer (ID, FName, SName, RoleID)
VALUES (2, 'Jack', 'Jackson', 1);
INSERT INTO CustomerRole (RID, Name)
VALUES (1, 'Manager');
此更新将导致触发器将 SName 更新为“Smith”
UPDATE ViewCustomer
SET FName = 'Peter'
WHERE ID = 1
这是它的 SQLFiddle。