當新的數據插入到BASETABLE我希望它使視圖(InsteadView)的觸發將數據插入到不同的表。它只適用於我手動將數據插入視圖,但不是當我插入數據到基表。SQL的,而不是在basetable觸發犯規觸發從視圖
CREATE TABLE BaseTable
(PrimaryKey int PRIMARY KEY IDENTITY(1,1),
Color nvarchar(10) NOT NULL,
Material nvarchar(10) NOT NULL,
ComputedCol AS (Color + Material)
)
GO
--Create a view that contains all columns from the base table.
CREATE VIEW InsteadView
AS SELECT PrimaryKey, Color, Material, ComputedCol
FROM BaseTable
GO
--Create an INSTEAD OF INSERT trigger on the view.
CREATE TRIGGER InsteadTrigger on InsteadView
INSTEAD OF INSERT
AS
BEGIN
--Build an INSERT statement ignoring inserted.PrimaryKey and
--inserted.ComputedCol.
INSERT INTO anotherTable
SELECT Color, Material
FROM inserted
END
GO
INSERT INTO BaseTable(顏色,材質 ) VALUES(N'Red 'N'Cloth')
- 查看INSERT語句的結果。 SELECT的PrimaryKey,顏色,材質 ,ComputedCol從另一個表
視圖的觀點是什麼? –
這是一個簡單的例子。真正的觀點是由15個表組成的。如果我能使這個工作,它會幫助我解決真實的問題。 – tdjfdjdj