-----------------------------------------------------------

New Message on MumbaiUserGroup

-----------------------------------------------------------
From: Pankil
Message 8 in Discussion


Hi Raj,

I have tried this too ... issue here is how to store column name?
Because the audit table here has same table structure ... where as in my case 
its common for all the tables which needs one insert per field.

Regards,
Pankil


From: "Rajashekar M" <[EMAIL PROTECTED]>
To: [email protected]
CC: [EMAIL PROTECTED]
Subject: Re: SQL Audit Trigger
Date: Fri, 24 Feb 2006 07:41:06 +0000




Hi,

Try using COLUMNS_UPDATED() , this is bitwise operations.
You need to derive at which column it got updated depending on the formula. If 
you have an access to SQL Server books you will be able to solve it properly.

I have copied the code or example provided in SQL Books Online: 


E. Use COLUMNS_UPDATED 

This example creates two tables: an employeeData table and an auditEmployeeData 
table. The employeeData table, which holds sensitive employee payroll 
information, can be modified by members of the human resources department. If 
the employee's social security number (SSN), yearly salary, or bank account 
number is changed, an audit record is generated and inserted into the 
auditEmployeeData audit table.

By using the COLUMNS_UPDATED() function, it is possible to test quickly for any 
changes to these columns that contain sensitive employee information. This use 
of COLUMNS_UPDATED() only works if you are trying to detect changes to the 
first 8 columns in the table.USE pubs
IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
   WHERE TABLE_NAME = 'employeeData')
   DROP TABLE employeeData
IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
   WHERE TABLE_NAME = 'auditEmployeeData')
   DROP TABLE auditEmployeeData
GO
CREATE TABLE employeeData (
   emp_id int NOT NULL,
   emp_bankAccountNumber char (10) NOT NULL,
   emp_salary int NOT NULL,
   emp_SSN char (11) NOT NULL,
   emp_lname nchar (32) NOT NULL,
   emp_fname nchar (32) NOT NULL,
   emp_manager int NOT NULL
   )
GO
CREATE TABLE auditEmployeeData (
   audit_log_id uniqueidentifier DEFAULT NEWID(),
   audit_log_type char (3) NOT NULL,
   audit_emp_id int NOT NULL,
   audit_emp_bankAccountNumber char (10) NULL,
   audit_emp_salary int NULL,
   audit_emp_SSN char (11) NULL,
   audit_user sysname DEFAULT SUSER_SNAME(),
   audit_changed datetime DEFAULT GETDATE()
   )
GO
CREATE TRIGGER updEmployeeData 
ON employeeData 
FOR update AS
/*Check whether columns 2, 3 or 4 has been updated. If any or all of columns 2, 
3 or 4 have been changed, create an audit record. The bitmask is: 
power(2,(2-1))+power(2,(3-1))+power(2,(4-1)) = 14. To check if all columns 2, 
3, and 4 are updated, use = 14 in place of >0 (below).*/

   IF (COLUMNS_UPDATED() & 14) > 0
/*Use IF (COLUMNS_UPDATED() & 14) = 14 to see if all of columns 2, 3, and 4 are 
updated.*/
      BEGIN
-- Audit OLD record.
      INSERT INTO auditEmployeeData
         (audit_log_type,
         audit_emp_id,
         audit_emp_bankAccountNumber,
         audit_emp_salary,
         audit_emp_SSN)
         SELECT 'OLD', 
            del.emp_id,
            del.emp_bankAccountNumber,
            del.emp_salary,
            del.emp_SSN
         FROM deleted del

-- Audit NEW record.
      INSERT INTO auditEmployeeData
         (audit_log_type,
         audit_emp_id,
         audit_emp_bankAccountNumber,
         audit_emp_salary,
         audit_emp_SSN)
         SELECT 'NEW',
            ins.emp_id,
            ins.emp_bankAccountNumber,
            ins.emp_salary,
            ins.emp_SSN
         FROM inserted ins
   END
GO

/*Inserting a new employee does not cause the UPDATE trigger to fire.*/
INSERT INTO employeeData
   VALUES ( 101, 'USA-987-01', 23000, 'R-M53550M', N'Mendel', N'Roland', 32)
GO

/*Updating the employee record for employee number 101 to change the salary to 
51000 causes the UPDATE trigger to fire and an audit trail to be produced.*/

UPDATE employeeData
   SET emp_salary = 51000
   WHERE emp_id = 101
GO
SELECT * FROM auditEmployeeData
GO

/*Updating the employee record for employee number 101 to change both the bank 
account number and social security number (SSN) causes the UPDATE trigger to 
fire and an audit trail to be produced.*/

UPDATE employeeData
   SET emp_bankAccountNumber = '133146A0', emp_SSN = 'R-M53550M'
   WHERE emp_id = 101
GO
SELECT * FROM auditEmployeeData
GO

F. Use COLUMNS_UPDATED to test more than 8 columns

If you must test for updates that affect columns other than the first 8 columns 
in a table, you must use the SUBSTRING function to test the proper bit returned 
by COLUMNS_UPDATED. This example tests for updates that affect columns 3, 5, or 
9 in the Northwind.dbo.Customers table.USE Northwind
DROP TRIGGER  tr1
GO
CREATE TRIGGER tr1 ON Customers
FOR UPDATE AS
   IF ( (SUBSTRING(COLUMNS_UPDATED(),1,1)=power(2,(3-1))
      + power(2,(5-1)))
      AND (SUBSTRING(COLUMNS_UPDATED(),2,1)=power(2,(1-1)))
      )
   PRINT 'Columns 3, 5 and 9 updated'
GO

UPDATE Customers
   SET ContactName=ContactName,
      Address=Address,
      Country=Country
GO


 

regards,
Raj




From: "Pankil" <[EMAIL PROTECTED]>
Reply-To: "MumbaiUserGroup" <[email protected]>
To: [EMAIL PROTECTED], [email protected]
Subject: Re: SQL Audit Trigger
Date: Thu, 23 Feb 2006 23:17:30 -0800


<STYLE>
ThmFgColumnHeader, A.FrameLink, A.HeaderLink, A.FooterLink, A.LgtCmd, A.MSNLink
{color:#FFFFFF;}
ThmFgTitleLightBk
{color:#FF6600;}
ThmFgSmallLight
{color:#ff0000;}
ThmFgNavLink, A.NavLink, A.ChildLink:hover
{color:#666699;}
ThmFgInactiveText, A.SystemLink
{color:#666666;}
ThmFgFrameTitle
{color:#FFFFCC;}
ThmFgMiscText, A.Cat, A.SubCat
{color:#336699;}
ThmFgCommand, A.Command, A.LargeCommand, A.MsgLink
{color:#003366;}
ThmFgHeader
{color:#333333;}
ThmFgDivider
{color:#CCCCCC;}
ThmBgStandard
{background-color:#FFFFFF;}
ThmBgUnknown1
{background-color:#FF6600;}
ThmBgFraming
{background-color:#666699;}
ThmBgUnknown2
{background-color:#666666;}
ThmBgHighlightDark
{background-color:#FFFFCC;}
ThmBgHighlightLight, #idToolbar, #tbContents
{background-color:#FFFFE8;}
ThmBgTitleDarkBk
{background-color:#F1F1F1;}
ThmBgAlternate
{background-color:#ECF1F6;}
ThmBgUnknown3
{background-color:#CCCCFF;}
ThmBgDivider
{background-color:#CCCCCC;}
ThmBgHeader
{background-color:#9999CC;}
ThmBgLinks
{background-color:#8696C9;}
ThmBgSharkBar
{background-color:#8696C9;}
ThmBgGlobalNick
{background-color:#9394A9;}
calfgndcolor
{color:#E00505;}
calbgndcolor
{color:#E00505;}
</STYLE>





New Message on MumbaiUserGroup



SQL Audit Trigger





Reply





 
Reply to Sender   Recommend 
Message 4 in Discussion 





From: Pankil 





Hi Raj,

Thanks for quick response. I have also tried with the IF UPDATE but here also I 
have to pass column name that means I have to loop through all the columns and 
check for individual column updated or not. Is there a way where I can find 
list of all columns being updated?

Regards,
Pankil


From: "Rajashekar M" <[EMAIL PROTECTED]>
To: [email protected]
CC: [EMAIL PROTECTED]
Subject: RE: SQL Audit Trigger
Date: Fri, 24 Feb 2006 06:31:56 +0000




Hi Pankil,

You can use the following keyword of SQL Server "IF UPDATE clause" to know 
which column got updated. It returns you true then it means the column got 
changed.

For detail example pls look into SQL Server Online books, you will find the 
exact sysntaxt to use.

If you feel still required help, let me know

Ex: CREATE TABLE my_table*
(a int NULL, b int NULL)
GO

CREATE TRIGGER my_trig
ON my_table
FOR INSERT
AS
IF UPDATE(b)
   PRINT 'Column b Modified'
GO


Regards,
Raj





From: "Pankil" <[EMAIL PROTECTED]>
Reply-To: "MumbaiUserGroup" <[email protected]>
To: "MumbaiUserGroup" <[email protected]>
Subject: SQL Audit Trigger
Date: Thu, 23 Feb 2006 21:25:49 -0800

<STYLE> ThmFgColumnHeader, A.FrameLink, A.HeaderLink, A.FooterLink, A.LgtCmd, 
A.MSNLink {color:#FFFFFF;} ThmFgTitleLightBk {color:#FF6600;} ThmFgSmallLight 
{color:#ff0000;} ThmFgNavLink, A.NavLink, A.ChildLink:hover {color:#666699;} 
ThmFgInactiveText, A.SystemLink {color:#666666;} ThmFgFrameTitle 
{color:#FFFFCC;} ThmFgMiscText, A.Cat, A.SubCat {color:#336699;} ThmFgCommand, 
A.Command, A.LargeCommand, A.MsgLink {color:#003366;} ThmFgHeader 
{color:#333333;} ThmFgDivider {color:#CCCCCC;} ThmBgStandard 
{background-color:#FFFFFF;} ThmBgUnknown1 
{background-color:#FF6600;} ThmBgFraming {background-color:#666699;} 
ThmBgUnknown2 {background-color:#666666;} ThmBgHighlightDark 
{background-color:#FFFFCC;} ThmBgHighlightLight, #idToolbar, #tbContents 
{background-color:#FFFFE8;} ThmBgTitleDarkBk {background-color:#F1F1F1;} 
ThmBgAlternate {background-color:#ECF1F6;} ThmBgUnknown3 
{background-color:#CCCCFF;} ThmBgDivider {background-color:#CCCCCC;} 
ThmBgHeader {background-color:#9999CC;} ThmBgLinks {background-color:#8696C9;} 
ThmBgSharkBar {background-color:#8696C9;} ThmBgGlobalNick 
{background-color:#9394A9;} calfgndcolor {color:#E00505;} calbgndcolor 
{color:#E00505;} </STYLE>




New Message on MumbaiUserGroup



SQL Audit Trigger





Reply





 
Reply to Sender   Recommend 
Message 1 in Discussion 





From: Pankil 



Hi,
I want to log an Audit for all records being inserted, update & deleted in 
another table of following format ..
 
Audit Table
-----------
Type Varchar(1)  --- This will store 'I', 'U' or 'D'
TableName Varchar(100) --Tablename
PK Varchar(400) --Stores primarykey name & value of that table
ColumnName Varchar(100) --Field
NewValue Varchar(200) --Value being inserted
OldValue Varchar(200) --Old value in case of Update/Delete
 
Say for example I am creating a trigger on Table Test having follwing structure
 
Test
----
PKTest  int   --
PKTest1 int  -- PKTest & PKTest1 being composite primary key
F1 Varchar(5)
F2 Varchar(10)
 
firing following queries in Test Table
1. insert into Test Values (1, 1, 'A', 'B')
2. insert into Test Values (1, 2, 'A1', 'B1')
3. Update Test Set F1 = 'C' Where PKTest = 1 and PKTest1 = 1
 
Now Requirement is following data needs to be logged in to Audit Table
 
Case 1. 
Type = 'I'
TableName = 'Test'
PK = 'PKTest=1|PKTest1=1'
ColumnName = F1
NewValue = 'A'
OldValue = Null
 
Case 2. 
Type = 'I'
TableName = 'Test'
PK = 'PKTest=1|PKTest1=2'
ColumnName = F1
NewValue = 'A1'
OldValue = Null
 
Case 3. 
Type = 'I'
TableName = 'Test'
PK = 'PKTest=1|PKTest1=1'
ColumnName = F1
NewValue = 'C'
OldValue = 'A'
 
I am able to achive this output but the trigger I used is using a loop through 
All columns and check if column is updated or not .. if yes then insert into 
audit log .. For current scenario there is no issues and works fine ... but 
issue arises when I want to write the trigger on a table having more than 60 
columns this slow down the inserts and update.
 
Is there any better way to write this trigger instead of looping through the 
column list and check for each column?
View other groups in this category. 


<STYLE> ADMSmLnk 
{font-family:verdana,system;color:#336699;text-decoration:none;} ADMSmLnk:hover 
{color:#ff6600;text-decoration:underline;} </STYLE>



Also on MSN:
Start Chatting | Listen to Music | House & Home | Try Online Dating | Daily 
Horoscopes








To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings. 

Need help? If you've forgotten your password, please go to Passport Member 
Services. 
For other questions or feedback, go to our Contact Us page. 

If you do not want to receive future e-mail from this MSN group, or if you 
received this message by mistake, please click the "Remove" link below. On the 
pre-addressed e-mail message that opens, simply click "Send". Your e-mail 
address will be deleted from this group's mailing 
list. 
Remove my e-mail address from MumbaiUserGroup. 




STILL SINGLE? Get married through MSN Matrimony. Join now for FREE! 
View other groups in this category. 









To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings. 

Need help? If you've forgotten your password, please go to Passport Member 
Services. 
For other questions or feedback, go to our Contact Us page. 

If you do not want to receive future e-mail from this MSN group, or if you 
received this message by mistake, please click the "Remove" link below. On the 
pre-addressed e-mail message that opens, simply click "Send". Your e-mail 
address will be deleted from this group's mailing 
list. 
Remove my e-mail address from MumbaiUserGroup. 


STILL SINGLE? Get married through MSN Matrimony. Join now for FREE! 

-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/mumbaiusergroup/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member 
Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you 
received this message by mistake, please click the "Remove" link below. On the 
pre-addressed e-mail message that opens, simply click "Send". Your e-mail 
address will be deleted from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to