0

我有一个表Products,我需要在该表上创建触发器以更新另一个表中的数据更改(新数据),该表Products_audit具有相同的列和相同的数据。

我试过以下:

CREATE TRIGGER Super 

ON Products 

AFTER INSERT, UPDATE

AS

UPDATE Product_audit (Column1,Column2, Column3, ... )

SELECT (Column1,Column2,Column3,...)

FROM Products

收到以下错误:

Server: Msg 170, Level 15, State 31, Procedure Super, Line 20
Line 20: Incorrect syntax near '('.
Server: Msg 170, Level 15, State 1, Procedure Super, Line 24
Line 24: Incorrect syntax near ','.

我使用 sql server 2000

我在触发器创建方面没有那么强,你能纠正我吗?我想知道我错在哪里?

4

2 回答 2

0

我认为你的触发器需要看起来像这样:

CREATE TRIGGER Super 

ON Products 

AFTER INSERT, UPDATE

AS

UPDATE Product_audit 

SET Column1 = p.Column1
SET Column2 = p.Column2

FROM Products p
于 2013-03-26T08:52:08.693 回答
0

您好,您必须创建两个不同的触发器,一个用于更新,一个用于插入 1) 用于插入

CREATE TRIGGER Super
ON Products
AFTER INSERT
AS
BEGIN

INSERT INTO Product_audit 
(Column1,Column2,...)
SELECT Column1,Column2,...
FROM Inserted

END

2) 更新

 CREATE TRIGGER Super
    ON Products
    AFTER Update
    AS
    BEGIN

    DECLARE @Column1 as varchar(10)
    DECLARE @Column2 as varchar(10)
    DECLARE @ID INT

    SELECT @ID=ID,@Column1=Column1,@Column2=Column2 FROM INSERTED 



   UPDATE Product_audit  SET Column1=@Column1,Column2=@Column2
   WHERE ID=@ID

    END
于 2013-03-26T08:53:23.273 回答