2

Possible Duplicate:
Create Trigger to log SQL that affected table?

Is possible in SQL Server 2008 to write a trigger which will put into my log table the query which triggered it? I will explain it on the example:

I have a table, TAB1, and log table LOG. I execute query which deletes rows from TAB1 and I want to have that query (or something else what could help me to identify who executed delete query on TAB1) in my LOG table.

Any idea how this can be achieved?

4

1 回答 1

0

我想到的唯一方法是通过 CONTEXT_INFO 显式传递过程名称,这将进入过程

declare @bin varbinary(128) = (cast(object_name(@@Procid) as varbinary(128))
set context_info @bin

并在触发器中使用它

declare @procName as nvarchar(max)
set @procName = cast(context_info() as nvarchar(max))

我不知道有什么其他方法可以做到这一点

编辑:我认为tham可能对你很有趣,clickie

于 2013-01-22T10:47:13.823 回答