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.
| Author |
Topic |
|
amodi
Yak Posting Veteran
83 Posts |
Posted - 2011-11-30 : 03:51:16
|
| Hello,I have a following tables:columns of table temp:id [PK]firstNameLastNameColumns of table temp_History:AuditId [PK]Id --> [PK] of temp tablefirstNameLastNameAudit_DateAudit_TypeI want to maintain the history of temp table in such a way that when any of the INSERT,UPDATE,DELETE operation happens, a record should be inserted in temp_History table in the following way:1. when a record is inserted in temp table audit_Type of temp_History table should be 'Insert'2. when any column is updated in temp table,audit_Type should be 'Update' 3. when a record is deleted in temp table, audit_Type should be 'Delete' I want to use triggers. I am new to triggers, so a code sample would be helpfulThanks. |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-30 : 06:36:56
|
| seehttp://www.nigelrivett.net/#TriggersIt's usual to log the before version and not the after - i.e. no logging for insert and only the before image for deletes.create trigger xxx on tempasdeclare @type varchar(1) = 'D'if exists (select * from inserted) if exists (select * from deletedselect @type = 'U'elseselect @type = 'I'if @type = 'I'insert temp_History (id, firstName, lastName, Audit_Date, Audit_Type)select id, firstName, lastName, getdate(), @typefrom insertedif @type = 'D'insert temp_History (id, firstName, lastName, Audit_Date, Audit_Type)select id, firstName, lastName, getdate(), @typefrom deletedif @type = 'U'insert temp_History (id, firstName, lastName, Audit_Date, Audit_Type)select t1.id, t1.firstName, t1.lastName, getdate(), @typefrom inserted t1join deleted t2on t1.id = t2.idand (t1.firstName <> t2.firstName or (t1.firstName is null and t2.firstName is not null) or (t1.firstName is not null and t2.firstName is null))and (t1.lastName <> t2.lastName or (t1.lastName is null and t2.lastName is not null) or (t1.lastName is not null and t2.lastName is null))union allselect t1.id, t1.firstName, t1.lastName, getdate(), @typefrom inserted t2join deleted t1on t1.id = t2.idand (t1.firstName <> t2.firstName or (t1.firstName is null and t2.firstName is not null) or (t1.firstName is not null and t2.firstName is null))and (t1.lastName <> t2.lastName or (t1.lastName is null and t2.lastName is not null) or (t1.lastName is not null and t2.lastName is null))==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
sureshkk
Starting Member
21 Posts |
Posted - 2011-11-30 : 07:45:20
|
| add for insert,update,delete statement before "as"create trigger xxx on tempfor insert,update,deleteas begin -----code end |
 |
|
|
amodi
Yak Posting Veteran
83 Posts |
Posted - 2011-11-30 : 08:32:01
|
| Thanks nigelrivett & sureshkk! |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2011-11-30 : 19:23:49
|
| See http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=170215 |
 |
|
|
|
|
|
|
|