TRIGGER TO MONITOR DELETES ON ALL THE TABLES

  • Hi guys,

    Is there a whay to create a trigger on all the tables to monitor data being deleted.I have writted someting like this:

    CREATE TABLE LOG_TABLE (Add_dttm datetime DEFAULT (GetDate()), TABLE_NAME sysname, Activity char(6))

    GO

    DECLARE @sql varchar(8000), @TABLE_NAME sysname

    SET NOCOUNT ON

    SELECT @TABLE_NAME = MIN(TABLE_NAME) FROM INFORMATION_SCHEMA.Tables

    WHILE @TABLE_NAME IS NOT NULL

    BEGIN

    SELECT @sql = 'CREATE TRIGGER [' + @TABLE_NAME + '_Usage_TR] ON [' + @TABLE_NAME +'] '

    + 'FOR DELETE AS '

    + 'IF EXISTS (SELECT * FROM deleted) '

    + 'INSERT INTO LOG_TABLE (TABLE_NAME,Activity) SELECT ''' + @TABLE_NAME + ''', ''DELETE''' + ' '

    + ' GO'

    SELECT @sql

    EXEC(@sql)

    SELECT @TABLE_NAME = MIN(TABLE_NAME) FROM INFORMATION_SCHEMA.Tables WHERE TABLE_NAME > @TABLE_NAME

    END

    SET NOCOUNT OFF

    Unfortunaley I also need to capture the data that was deleted.IS there a way i can have this captured?

    thanks in advance.

  • Thank you praddep for the reply.Unfortunately I cannot use DDL triggers because they do not capture data being deleted from a table .They are usefull only when objects are deleted ,dropped or altered.I also need to capture the data that was deleted .

  • Add an XML column to your LOG_TABLE and add a FOR XML subquery:

    'INSERT INTO LOG_TABLE (TABLE_NAME,Activity) SELECT ''' + @TABLE_NAME + ''', ''DELETE'''

    + ' (SELECT * FROM deleted FOR XML PATH, TYPE)'

    Of course you understand that this is going to consume a *lot* of diskspace, right?

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • DDL triggers do not fire for data changes. They fire for DDL (data definition language) changes only.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • thanx for the reply

    I am getting syntax errors when trying to inplement XML

    The select list for the INSERT statement contains fewer items than the insert list. The number of SELECT values must match the number of INSERT columns.

    and

    Incorrect syntax near the keyword 'FOR'

  • You are correct Gila.Is there a way I can implement this by modifying my existing code?At the moment it captures the table on which the data was being deleted but i would also need to capture the data that was deleted.

    thanks in advance.

  • Hmm, probably needs this extra comma:

    'INSERT INTO LOG_TABLE (TABLE_NAME,Activity) SELECT ''' + @TABLE_NAME + ''', ''DELETE'''

    + ', (SELECT * FROM deleted FOR XML PATH, TYPE)'

    -- ^

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • RBarry it is still giving me error saying:

    The select list for the INSERT statement contains more items than the insert list. The number of SELECT values must match the number of INSERT columns.

    what does the XML PATH and TYPE tables contain?Is the error occuring because we are saying select *?

  • OK, this is a little bit hard to test with out your table. Try this:

    'INSERT INTO LOG_TABLE (TABLE_NAME,Activity, YourXMLcolumn) SELECT ''' + @TABLE_NAME + ''', ''DELETE'''

    + ', (SELECT * FROM deleted FOR XML PATH, TYPE)'

    -- ^

    Note: you have to make the new column name in the INSERT list match what you actually named the column in your table.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Proactively and helpfully, I'm not sure why you want to do this, because you haven't explained it.

    If you want to analyse all deletes, there are log analysis tools or cheaper just using profiler, that can help you do this.

    Have I missed your point somewhere?

    Tim

    .

  • I had the same need: record the DML commands on a table in one database. I have used the auditing mechanisms.

    With SQL Server 2008 auditing support is native thanks to the command "CREATE AUDIT" with SQL Server 2000 or 2005 we must build a custom solution based on SQL Trace (SQL Profiler is based on SQL Trace), you can find from this link:

    http://community.ugiss.org/blogs/sgovoni/archive/2008/09/18/meccanismi-di-auditing.aspx

    I used the trigger only for UPDATE commands to know the value that was an attribute before the change and the value which took the attribute after the change.

    Bye

  • Tim,

    I am thankfull that somebody has questioned my intentions.I understand that using triggers are going to be expensive.I am new to this job and I don't know of too many options.In your opinion what is the best way to analyse and record data deletes?Is SQL Broker an option?

    thanks

  • I think it would be fair to say that logging this level of detail from ALL your tables is an unusual requirement, and I would question why you want to do it.

    It's more common to just need some of the tables and when I need to do this I tend to use a specific trigger to hold the audit data in the same structure as the table itself with an additional column to say whether an insert, update, or delete was done.

    Assuming that you do have a definite requirement to do this for some reason, here is a link to a way of doing it generically similar to the one that RBarryYoung was proposing. Bear in mind XML is VERY wordy not least because it effectively records the column names against every row and will therefore consume a lot of space.

    http://www.sqlservercentral.com/scripts/SQLInsider+Scripts/32008/

    An alternative is to use a log analyzer after the event to analyze the log, E.g. apexsql log. Haven't used it myself so I can't make a recommendation, but it looks like a trial version is available.

    http://www.apexsql.com/sql_tools_log.asp

    Tim

    .

  • There are several 3rd party auditing products - ApexSQL and RedGate both have them IIRC. I think Paul Randall wrote a freebie script that will create the necessary triggers and tables as well you could possibly modify to just do delete auditing.

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

Viewing 15 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic. Login to reply