插入触发器后MSSQL无法正常工作

时间:2014-11-14 02:19:48

标签: sql-server

USE [mydatabase]
GO
/****** Object:  Trigger [dbo].[trgAfterInsert]    Script Date: 14.11.2014 2:30:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[trgAfterInsert] ON [dbo].[MYTABLE] 
FOR INSERT
AS
        declare @ID int;
        declare @DATE date;
    declare @WHEN smalldatetime;
    declare @WHO NVARCHAR(50);
    declare @WHAT NVARCHAR(max); 
    declare @AUDIT_ACTION varchar(100);


    select @ID        = i.ID from inserted i;   
    select @DATE          = i.DATE from inserted i;
    select @WHEN          = i.WHEN from inserted i;
    select @WHO           = i.WHO from inserted i;
    select @WHAT          = i.WHAT from inserted i;



      UPDATE MYTABLE 
      SET WHEN = GETDATE(),
      WHO      = USER_NAME(USER_ID())
      FROM dbo.MYTABLE  
      INNER JOIN inserted ON MYTABLE.ID = inserted.ID

      insert into AUDIT_LOG           

(DATE,WHEN,WHO,WHAT,USER,AUDIT_ACTION,AUDIT_TIMESTAMP) 
values

(@DATE,@WHEN,@WHO,@WHAT,USER_NAME(USER_ID()),'NEW ENTRY',GETDATE());

运行时,“何时”和“谁”不会插入AUDIT_LOG。

我在这里要做的是首先更新表然后登录 共

我做错了什么?

1 个答案:

答案 0 :(得分:1)

我认为SQL编辑器正在处理专栏' WHEN'好像它是一个保留的关键字,所以试着把它放在括号之间,如[WHEN]