SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 using a trigger
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jassie
Constraint Violating Yak Guru

296 Posts

Posted - 01/22/2013 :  22:00:00  Show Profile  Reply with Quote
In a C# 2010 desktop application, I need to add an audit table to an application that would be similar to the 'main' table. Basically whenever anyting is changed in the 'main' table I need to show the corresponding change in the 'audit' table. The 'audit' table will be a mirror copy of the 'main' table. The only difference is one table is called 'main' table and the other table is called the 'audit' table.

**Note these table reside in a sql server 2008 r2 database.

Would you setup a trigger? If so, can you show me the sql you would use?

What sql would you use?

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 01/23/2013 :  00:58:39  Show Profile  Reply with Quote
its as simple as
insert into audit select column1,column2,.. from main

have a look at CREATE TRIGGER statement for syntax

http://msdn.microsoft.com/en-us/library/ms189799.aspx

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

Go to Top of Page

jassie
Constraint Violating Yak Guru

296 Posts

Posted - 01/23/2013 :  10:55:10  Show Profile  Reply with Quote
Can you explain to me how the trigger is executed? Also can you tell me if you would add any extra columns to the trigger like, what type of a change is made, who made the change?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 01/23/2013 :  13:10:20  Show Profile  Reply with Quote
trigger will be fired automatically each time you do the DML action(INSERT/UPDATE/DELETE)
You can add extra columns inside if you want. Type of change can be determined based on internal temporary tables DELETED and INSERTED.


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

Go to Top of Page

jassie
Constraint Violating Yak Guru

296 Posts

Posted - 01/27/2013 :  20:18:55  Show Profile  Reply with Quote
Thanks!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 01/27/2013 :  23:59:21  Show Profile  Reply with Quote
welcome

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

Go to Top of Page

Zath
Constraint Violating Yak Guru

USA
284 Posts

Posted - 01/28/2013 :  13:46:05  Show Profile  Visit Zath's Homepage  Reply with Quote
If I amy add my two cents worth here:

When using a trigger and in any stored procedure that uses that table with a trigger attached, if you get any identity from the table from an insert, be sure to use SCOPE_IDENTITY().

Do not use @@Identity as this will get the identity of your history or archive table that the trigger uses.

Here is a nice article on the subject:

http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/


Zath

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000