Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 audit via trigger or sproc

Author  Topic 

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-10-05 : 13:12:47
greetings

i have created an audit table for each of the tables in my database. i went the route of one audit table per table for ease of management and speed.

i want to have more control of the insertion of values to these audit tables so I would prefer to use sprocs rather than triggers

I have implemented the following approach and it is working


BEGIN TRANSACTION;
BEGIN TRY

SET NOCOUNT ON;

INSERT INTO dbo.payments
( payment_id ,
payment_date
)
VALUES ( @payment_id ,
GETDATE()
)
SET @primary_key = SCOPE_IDENTITY()

EXEC dbo.spAudit @table_name = N'payments',@Action = N'INSERT', @primary_key = @primary_key

COMMIT TRANSACTION @TranName;
END TRY


I want to use on sproc sp_audit to do all of my auditing. This will mean I will have to use dynamic sql. What would you suggest I use triggers or sproc?

Thanks

If you don't have the passion to help people, you have no passion

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-05 : 13:30:47
Auditing is typically done in triggers.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-10-05 : 13:34:38
anyways using dynamic sql is bad idea. Better to write audit procs specific to tables.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-05 : 13:36:33
Agreed about the dynamic sql, however it depends on what spAudit is doing. If it's just a simple insert command, then using dynamic sql is not a performance concern.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2010-10-05 : 13:38:31
generally speaking:
If you want to guarantee that any and all changes to the table get logged then a trigger (for each subject table) is the way to go. Even if your policy is to only modify the table via a stored proc there will certainly be a case where an admin gets in there and does something. That change would be lost without a trigger.

a trigger is technically a stored procedure so under the sheets there is no difference. A trigger will be implicitly part of the user transaction so you don't need an explicit transaction. However, for that reason, you should make the trigger code as brief and simple as possible. The only code should be the insert to the audit table. And the audit table should have no possibility of violating a constraint.

What "control" will you be losing by using a trigger rather than an SP?

Be One with the Optimizer
TG
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-05 : 13:42:49
yosiaz, can you post spAudit? Hopefully it's simple and can follow exactly what TG has said, which is the best practice.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-10-05 : 14:13:50
wow i go to get coffee and all these responses, thanks! The reason I wanted to do it in sproc is in order to make it generic and have control over what happens under the hood ,it's a mechanic thing since I used to be a mechanic, I want, I need, I need control :) (What About Bob) I am just a control freak that is all. What TG says is on the money if someone made changes to table behind the scenes it will not be captured. It will be a very very simple trigger. The spAudit is already getting to be hairy with concatenation and all.
or I could go the visakh way and create a sproc for each table. I guess there is a trade off. Looks like I might need to consider triggers, I just do not like triggers, no particular reason really.

Thank you all very much!

If you don't have the passion to help people, you have no passion
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2010-10-05 : 14:31:33
quote:
Originally posted by yosiasz



BEGIN TRANSACTION;
BEGIN TRY

SET NOCOUNT ON;

INSERT INTO dbo.payments
( payment_id ,
payment_date
)
VALUES ( @payment_id ,
GETDATE()
)
SET @primary_key = SCOPE_IDENTITY()

EXEC dbo.spAudit @table_name = N'payments',@Action = N'INSERT', @primary_key = @primary_key

COMMIT TRANSACTION @TranName;
END TRY




this is flawed. there's no rollback if the TRY fails

also, as said b4, triggers are the way to go.
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-10-05 : 15:02:35
russell i did not post the rollback that is why.

thanks

If you don't have the passion to help people, you have no passion
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2010-10-05 : 15:11:03
quote:
Originally posted by yosiasz
The reason I wanted to do it in sproc is in order to make it generic and have control over what happens under the hood



You are not the first person to want to write generic triggers.

quote:
Originally posted by yosiasz
The reason I wanted to do it in sproc is in order to make it generic and have control over what happens under the hood



You will not be the last person to learn that these two goals "generic" and "control" are actually at odds with eachother.

________________________________________________
If it is not practically useful, then it is practically useless.
________________________________________________
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-10-05 : 16:52:05
i have seen the light blindman, thank you!

If you don't have the passion to help people, you have no passion
Go to Top of Page
   

- Advertisement -