Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2005 捕获发送方和命令的删除或更新时触发_Sql Server 2005 - Fatal编程技术网

Sql server 2005 捕获发送方和命令的删除或更新时触发

Sql server 2005 捕获发送方和命令的删除或更新时触发,sql-server-2005,Sql Server 2005,我需要找出是谁在删除/更新表中的数据,时间,使用什么程序,以及发送到导致修改的数据库的命令 通过谷歌搜索和询问一些同事,推荐的方法是删除触发器。我知道如何创建触发器,例如: create trigger whodunit on THETABLE for delete as begin insert into MyAuditTbl(moddate, ... end 但是如何获取发送到DB查询/存储过程的命令、应用程序名称、IP地址等?我找到并自定义了它以满足我的需要: create tr

我需要找出是谁在删除/更新表中的数据,时间,使用什么程序,以及发送到导致修改的数据库的命令

通过谷歌搜索和询问一些同事,推荐的方法是删除触发器。我知道如何创建触发器,例如:

create trigger whodunit
on THETABLE
for delete
as begin
    insert into MyAuditTbl(moddate, ...
end
但是如何获取发送到DB查询/存储过程的命令、应用程序名称、IP地址等?

我找到并自定义了它以满足我的需要:

create trigger AuditTHETABLE
    on THETABLE
    for delete, update
as begin
    set nocount on

    declare @shouldlog bit, @insertcount bigint, @deletecount bigint
    select
        @shouldlog = 1,
        @insertcount = (select count(*) from inserted),
        @deletecount = (select count(*) from deleted)

    -- if no rows are changed, do not log
    if @insertcount < 1 and @deletecount < 1 begin
        select @shouldlog = 0
    end

    -- ... other checks whether to log or not

    if @shouldlog = 1 begin
        -- prepare variable to capture last command
        declare @buffer table (
            eventtype nvarchar(30),
            parameters int,
            eventinfo nvarchar(4000)
        )

        -- use DBCC INPUTBUFFER to capture last command
        -- unfortunately only the first 255 characters are captured
        insert @buffer
        exec sp_executesql N'DBCC INPUTBUFFER(@@spid) WITH NO_INFOMSGS'

        declare @lastcommand varchar(max)
        select @lastcommand = eventinfo from @buffer

        -- insert into audit table
        insert into myauditlog(
            eventdate, tablename, hostname,
            appname, insertcount, deletecount, command
        ) values(
            getdate(),
            'THETABLE',
            host_name(),
            app_name(),
            @insertcount,
            @deletecount,
            @lastcommand
        )
    end
end