我正在尝试使用以下近似形式编写SQL Trigger:
CREATE TRIGGER TableABSync ON TableA
FOR INSERT AS
INSERT INTO TableB
(col1, col2, col3, col4)
SELECT
PK_ColA, ColB, ColC - ColD, ColE * TableB.Col3
FROM inserted
所以我想使用新生成的Col3来计算Col4。我是否必须分阶段这样做?
干杯
答案 0 :(得分:1)
为什么不直接使用类似的东西:
CREATE TRIGGER TableABSync ON TableA
FOR INSERT AS
INSERT INTO TableB
(col1, col2, col3, col4)
SELECT
PK_ColA, ColB, ColC - ColD, ColE *( TableB.ColC - TableB.ColD)
FROM inserted
答案 1 :(得分:0)
该问题与触发器或插入无关 - 您无法在同一SELECT
子句中引用SELECT
子句中的计算值。
也许:
CREATE TRIGGER TableABSync ON TableA
FOR INSERT AS
INSERT INTO TableB
(col1, col2, col3, col4)
SELECT
PK_ColA, ColB, ColC - ColD as Col3, ColE * .Col3
FROM
(
SELECT PK_ColA, ColB, ColC - ColD as Col3,ColE
FROM inserted
) t