需要为审计跟踪创建触发器,获得“不正确的语法?”

时间:2013-03-26 08:44:50

标签: sql sql-server sql-server-2000

我有一个表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

我在触发器创建方面不是那么强大你能纠正我吗我想知道我哪里错了?

2 个答案:

答案 0 :(得分:0)

我认为你需要看起来像这样:

CREATE TRIGGER Super 

ON Products 

AFTER INSERT, UPDATE

AS

UPDATE Product_audit 

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

FROM Products p

答案 1 :(得分: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