Discussion Locked

Auditrail Feature in SQL server

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I wanna have the Transaction record of all my project Master Tables in some table whih will be created in database.
The Columns of table I need is :
1. User_id
2. User_Name
3. Table_Name ( on which opertaion is performed)
4. Operation Performed
5. Date and Time of Operation
6. whether operatin is successful or not.

In oracle 8i there is a feature which automatically does this (Set Audit trail On )...but in SQL server I am unaware of any such feature. Please Help me out.

Even a trigger on particular table can do this operation. If so, please tell me HOW CAN I GET USER_NAME AND USER_ID who is performing operation on table.

5 replies

by limbo

Search 'SQL Books Online', or 'msdn.microsoft.com' for entries of 'xp_sqltrace'.

This extended stored procedure has several functions, including 'audit' & 'trace'.

by limbo

What, don't what to use 'xp_sqltrace'?

OK why don't you consider using the 'CURRENT_USER' function instead, in a trigger.

i.e.

SELECT CURRENT_USER

In the context of the current transaction, this function will correlate the DB Assigned UID number for the connection with the 'sysusers' table...

Therefore, include it in an insert trigger, and the user who fired the event can be captured...

For the table name, well that would be a constant if you were building a trigger...
Again the operation performed would be a constant in the context of the trigger function...

Date and Time, simple, just GETDATE()

Determine transaction success or failure perform @@ERRORLEVEL checks, and @@ROWCOUNT (I prefer to return this upto the client layer through ADO).

So to summarise:

SELECT
CURRENT_USER AS 'User Name',
'This Table' AS 'Table Effected',
'Insert' AS 'Trigger Mode',
GETDATE() AS 'System Time',
@@ERROR AS 'Last Error',
@@ROWCOUNT AS 'Rows Effected'

This discussion is closed to new replies.