这是原表
C1 C2 C3
NULL 10 1
30 NULL 2
20 20 3
要求用instead of trigger 阻止C3这一栏的任何更新,其他的允许。
以前都是当值大于或小于特定的数就触发,但是这道题要求触发当更新C3中的任何一个。
求教~
------解决思路----------------------
数据表加一个ID列。
IF OBJECT_ID('TA') IS NOT NULL
DROP TABLE ta
GO
CREATE TABLE TA(ID INT IDENTITY(1,1) PRIMARY KEY, C1 INT, C2 INT, C3 INT)
GO
INSERT INTO TA VALUES(NULL, 10, 1)
INSERT INTO TA VALUES(30, NULL, 2)
INSERT INTO TA VALUES(20, 20, 3)
GO
SELECT * FROM TA
GO
IF OBJECT_ID('TRI_TA') IS NOT NULL
DROP TRIGGER TRI_TA
GO
CREATE TRIGGER TRI_TA
ON TA
INSTEAD OF UPDATE
AS
UPDATE TA
SET C1 = A.C1 , C2 = A.C2
FROM TA
JOIN inserted A
ON TA.ID = A.ID
GO
UPDATE TA
SET C1 = 23, C2 = 32, C3 = 99
where ID = 2
SELECT * FROM TA