zoukankan      html  css  js  c++  java
  • 用触发器追踪到底是哪个存储过程删了数据

    有朋友用SQL Server开发了很多存储过程,这些存储过程会访问表Students。忽然有天发现表Students的数据少了,想知道是哪个存储过程删了数据。于是他在表Students上创建了Delete触发器。

    如何用触发器追踪到底是哪个存储过程删了数据呢?我们用实例来演示下。

    一,创建表Students 和数据

    create table students(
    id integer primary key,
    name varchar(20),
    age int
    ); insert into students values(1, 'Flower', 18); insert into students values(2, 'John', 19); insert into students values(3, 'Jeff', 17);

    二,创建存储过程

    create procedure del_student @del_id integer
    as
    begin
       delete from tosska.dbo.students where id =@del_id;
    end;

    三,创建Delete触发器

        通过dbcc inputbuffer 命令,将触发Delete触发器的程序信息,写入日志表 tosska.dbo.log_students。

    create trigger students_delete
    on students
    after delete
    as
    declare @info nvarchar(4000);
    declare @source table (EventType nvarchar(30), Parameters int, EventInfo nvarchar(4000));
    begin
      if object_id(N'tosska.dbo.log_students', N'U') is null
         create table tosska.dbo.log_students(spid int, log_time datetime, application nvarchar(128), info varchar(4000)); 
      insert into @source exec ('dbcc inputbuffer (' + @@spid + ') with no_infomsgs');
      select top 1 @info = EventInfo from @source;
      insert into tosska.dbo.log_students values(@@spid, getdate(), app_name(), @info);
    end;

    四,尝试删除数据

    exec dbo.del_student 1;

    五,查看日志表

     

      applicaiton字段记录了客户端程序名字,info字段记录了删除数据的存储过程,看起来很完美。

    SQL优化工具,让您节省时间,专注公司业务
  • 相关阅读:
    IOS6.0 应用内直接下载程序 不需跳转AppStore
    维护Product
    CRM Service summary 1
    "No update of sales order X from purchase order (error V1 154)."
    Organizational Management
    懂PMP的好处
    Buying Center
    更新
    Business Partner定义,以及与Account的区分
    Fact Sheet
  • 原文地址:https://www.cnblogs.com/dbexpert/p/15237637.html
Copyright © 2011-2022 走看看