0

我正在尝试使用以下近似形式编写 SQL 触发器:

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。我必须分阶段进行吗?

干杯

4

2 回答 2

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
于 2012-08-03T10:48:23.270 回答
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
于 2012-08-03T10:54:59.827 回答