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.
How can I achieve this?
Please illustrate with an example.
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
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
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
ASKER
Thanks
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.m
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.m
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.m
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