----------------------------------------------------------- New Message on MumbaiUserGroup
----------------------------------------------------------- From: Pankil Message 4 in Discussion 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! ----------------------------------------------------------- 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]
