0

when using a trigger in SQL SERVER 2005 the eventdata() always return a empty value.

only the date inserted in the Audit table, other fields are NULL valus

pls help me

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER [dbo].[TrigDip]
   ON  [dbo].[Dip]
   AFTER INSERT,DELETE,UPDATE
AS 
BEGIN

DECLARE @ed XML
SET @ed = EVENTDATA()

DECLARE @db varchar(1000)

set @db =EVENTDATA().value('(/EVENT_INSTANCE/DatabaseName)[1]', 'varchar(256)')
PRINT 'CREATE TABLE Issued.'

INSERT INTO Audit (PostTime, DatabaseName, Event, ObjectName, TSQL, Login) 
VALUES
(
    GetDate(),
    @db,              
    EVENTDATA().value('(/EVENT_INSTANCE/EventType)[1]', 'nvarchar(100)'),
    EVENTDATA().value('(/EVENT_INSTANCE/ObjectName)[1]', 'varchar(256)'),
    EVENTDATA().value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'nvarchar(2000)'),
    CONVERT(nvarchar(100), CURRENT_USER)
) 

END
4

1 回答 1

1

MSDN 上的 EVENTDATA状态

仅当直接在 DDL 或登录触发器内部引用时,EVENTDATA 才会返回数据。如果 EVENTDATA 被其他例程调用,则返回 null,即使这些例程由 DDL 或登录触发器调用也是如此。

您上面的触发器是 DML AFTER 触发器。

你需要

CREATE TRIGGER TRG_CreateTable
ON DATABASE 
FOR CREATE_TABLE 
...
于 2013-06-25T09:19:58.143 回答