Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 1234»»»

Creating a generic audit trigger with SQL 2005 CLR Expand / Collapse
Author
Message
Posted Thursday, July 13, 2006 11:59 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, November 6, 2013 2:24 PM
Points: 69, Visits: 182
Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/dziffer/creatingagenericaudittriggerwithsql2005clr.asp
Post #294216
Posted Wednesday, August 2, 2006 7:42 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, January 9, 2007 8:00 AM
Points: 1, Visits: 1
I have to say that, as far as database design and good coding practices go, this solution is definitely not something that I would recommend. Your idea of having a single audit table with Row-Id (1) works only for single-column primary keys, and (2) would result in HUGE overhead for inserts, updates, and deletes whenever more than one column is affected. If I insert one row into a table that has 80 columns, your trigger would add the additional overhead of inserting 80 rows, one per column. No way would a well-educated DBA allow or encourage this to occur.

This type of coding/behavior is a more OO approach to database development and I've seen a lot of inexperienced-database-developers-who-are-good-application-developers design such tables. OO to Relational mapping is hard, no question, but a relational database is not object-oriented and, when you try to make it such (as with this generic design), you are causing more headaches than creating an audit table and trigger for each table. IMO, this is an example of the reasons most DBAs will not allow CLR code to be executed on their server. This is just an example of a .NET programmer doing something in .NET that should be done in T-SQL.

Scott Whigham
LearnSqlServer.com - tutorials for SQL Server 2005 and SQL 2000
Post #298934
Posted Wednesday, August 2, 2006 8:17 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Thursday, November 27, 2014 4:23 AM
Points: 20,584, Visits: 9,624

Totally agree with Scott on this one.

What happens if you delete 250 000 rows from a table with 80 columns???

How do you recreate the whole history of a single row with a select query??? (All the columns of the row at each version of its life).

 

BTW I didn't read the whole thing but I seems you are having problems identifying the base table name from within the trigger.  Maybe you can use this code somehow to extract the basetable name (from sql 2000).

Select Object_name(Parent_obj) as BaseTableName from dbo.SysObjects where id = @@PROCID

Where @@PROCID is the id of the trigger in sysobjects.  This variable is available and set within the trigger and any other procedure for that matter.

Post #298963
Posted Wednesday, August 2, 2006 8:33 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 12:14 PM
Points: 148, Visits: 655

Agree.  Stick to VB.

It does give some good insight into .NET, tho.




Post #298973
Posted Wednesday, August 2, 2006 9:52 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, March 16, 2007 10:30 AM
Points: 3, Visits: 1
I would have gotten more out of the note if the lines didn't extend past the limits of my fully expanded browser. Recommend either not preformatting the text or using a more reasonable line width.

Post #299020
Posted Wednesday, August 2, 2006 10:01 AM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, July 25, 2013 1:00 AM
Points: 53, Visits: 212
My god... I have to agree with the previous posts. This type of trigger is a wholly bad idea. Besides the massive amounts of overhead in splitting each column out from the table for each update, there will also be the contention between tables as evey table in the database attempts to write to a single audit table.

And as RGR mentioned, how do you get this data back out overly complex SQL queries?
Post #299022
Posted Wednesday, August 2, 2006 10:46 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, November 4, 2014 9:24 AM
Points: 103, Visits: 12
Any of you nay sayers (not that you are wrong) have any better plans?  What do YOU suggest?


Post #299051
Posted Wednesday, August 2, 2006 11:14 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Thursday, November 27, 2014 4:23 AM
Points: 20,584, Visits: 9,624

A solution to this problem has already been created on this site.  Search the script section with the keywords triggers and (generated, concatenated... sorry don't remember the name).

 

Basically it's a script that creates an audit table for each table in the system.  It also creates the trigger that audits the data.  I don't remember if it creates only the code or if it generates the objects as well.  But it was working great as far as I remember.

Post #299062
Posted Wednesday, August 2, 2006 12:38 PM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, July 25, 2013 1:00 AM
Points: 53, Visits: 212
I have a template that I put together for CodeSmith that I use. It works for a single or multiple tables. Though I should probably update it for SQL Server 2005 to possibly use an audit schema for the audit tables.
Post #299089
Posted Wednesday, August 2, 2006 1:06 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, June 26, 2014 7:44 AM
Points: 134, Visits: 178
I don't think it's a good idea to audit ALL tables in the database.

For the most important tables (which usually are small so the overhead is minimal), I use the following solution (SQL 2005):

The logging table (only one for the entire database):

create table DMLLogs(
DMLLogID int identity primary key,
TableName nvarchar (128),
DateCreated datetime DEFAULT (getdate()),
OldValues xml NULL,
NewValues xml NULL)


The trigger for the table which you want to audit; The only thing that needs to be changed for another table is the table name. You can use the solution presented early in this post:
Select @TableName = Object_name(Parent_obj)
from dbo.SysObjects
where id = @@PROCID;
in this case the trigger will be exactly the same for all audited tables

create trigger MyTable_T_Log on MyTable
for insert, update, delete
as
declare @i xml, @d xml
set @i = (select * from inserted for xml auto)
set @d = (select * from deleted for xml auto)
exec LogDMLEvent
@TableName = 'MyTable',
@Deleted = @D,
@Inserted = @I

The procedure LogDMLEvent is:
create procedure LogDMLEvent
@TableName sysname,
@Deleted xml,
@Inserted xml
as
if @Deleted is not null or @Inserted is not null
insert into DMLLogs (TableName, OldValues, NewValues)
values (@TableName, @Deleted, @Inserted)

you can include in this procedure (and in the DMLLogs table) also the user name, connection info ....

So in the logging table you will have xml representations of inserted and deleted tables. You can very easy put them into a view to mimic the original table structure (one view for each table).
Post #299093
« Prev Topic | Next Topic »

Add to briefcase 1234»»»

Permissions Expand / Collapse