Link to home
Start Free TrialLog in
Avatar of John86a
John86a

asked on

A trigger to clone a table?

When inserted/update the contents of Table A should go to Table B. I plan on making Table B the history of Table A, so, rows in Table B should not be deleted when they're deleted in Table A. The concept is easy, 'History'.

How can I achieve this?

Please illustrate with an example.

Thanks.
Avatar of Imran Javed Zia
Imran Javed Zia
Flag of Pakistan image

Hi,
Just use After Insert trigger on Table A
and when you are in tigger insert the row in Table B

CREATE TRIGGER [ schema_name . ]trigger_name
ON { table | view }
[ WITH <dml_trigger_option> [ ,...n ] ]
{ FOR | AFTER | INSTEAD OF }
{ [ INSERT ] [ , ] [ UPDATE ] [ , ] [ DELETE ] }
[ WITH APPEND ]
[ NOT FOR REPLICATION ]
AS { sql_statement  [ ; ] [ ,...n ] | EXTERNAL NAME <method specifier [ ; ] > }

<dml_trigger_option> ::=
    [ ENCRYPTION ]
    [ EXECUTE AS Clause ]

<method_specifier> ::=
    assembly_name.class_name.method_name

Trigger on a CREATE, ALTER, DROP, GRANT, DENY, REVOKE, or UPDATE STATISTICS statement (DDL Trigger)
CREATE TRIGGER trigger_name
ON { ALL SERVER | DATABASE }
[ WITH <ddl_trigger_option> [ ,...n ] ]
{ FOR | AFTER } { event_type | event_group } [ ,...n ]
AS { sql_statement  [ ; ] [ ,...n ] | EXTERNAL NAME < method specifier >  [ ; ] }

<ddl_trigger_option> ::=
    [ ENCRYPTION ]
    [ EXECUTE AS Clause ]

<method_specifier> ::=
    assembly_name.class_name.method_name

Trigger on a LOGON event (Logon Trigger)
CREATE TRIGGER trigger_name
ON ALL SERVER
[ WITH <logon_trigger_option> [ ,...n ] ]
{ FOR| AFTER } LOGON  
AS { sql_statement  [ ; ] [ ,...n ] | EXTERNAL NAME < method specifier >  [ ; ] }

<logon_trigger_option> ::=
    [ ENCRYPTION ]
    [ EXECUTE AS Clause ]

<method_specifier> ::=
    assembly_name.class_name.method_name
 

please follow the links for detail and example:
http://www.kodyaz.com/articles/sql-trigger-example-in-sql-server-2008.aspx
http://msdn.microsoft.com/en-us/library/ms189799.aspx


Thanks
ASKER CERTIFIED SOLUTION
Avatar of Lowfatspread
Lowfatspread
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you may also like to consider using the audit table feature of sql server...

CDC - Change data capture may also be of use...

similarly you could use replication (to another database i'd suggest)
where you would ignore deletions , and write a custom stored procedure to wirte a delete row... you'd still need to create the tables  with the additional columns and write customised procedures.
Avatar of gladfellow
gladfellow

Without triggers, you may want to consider an alternative for implementing a simple 'History' requirement.

Have only one table in which you insert, update and SOFT-DELETE. By SOFT-DELETE, I mean do not delete any row, but simply mark it as 'Deleted' using an extra column. This would be the primary table as well as your audit table. However, for all your active query needs, create a view that 'SELECTS FROM THE PRIMARY TABLE WHERE RECORD STATUS IS NOT 'Deleted''.

This may satisfy a simple requirement without having to overload each insert and update with corresponding triggers to clone those inserts and updates.

- Venkat
Avatar of John86a

ASKER

Thanks