When new data is inserted into BASETABLE I want it to make the View (InsteadView)'s trigger to insert data into a different table. It only works when I insert data manually to the view, but not when i insert data into the 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 (Color, Material) VALUES (N'Red', N'Cloth')
--View the results of the INSERT statement. SELECT PrimaryKey, Color, Material, ComputedCol FROM another table