• subscribe
October 23, 2002 12:00 AM

Inserting Records into Two Tables at Once

SQL Server Pro
InstantDoc ID #26856
Downloads
26856.zip

How can I use one INSERT statement and an INSERT trigger to add records to two tables at the same time?

To add records to two tables at the same time, you need to direct your INSERT statement at the first table. Then, on that table, create an INSERT trigger that uses the inserted table to add records to the second table. Listing 3 shows sample code that performs these operations.

The most important tip to remember about writing this statement is that inserting records into one table, activating the INSERT trigger, and adding records to a second table take place in the same transaction and at the same isolation level. Therefore, you need to avoid creating a long-running trigger that delays your users. Note that this answer assumes that the two tables sustain a data relationship. Otherwise, wrapping two separate INSERT statements in a transaction would be more efficient.



ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here
  • SP1?
    I know there is a SP1 for SQL 2008 R2 available....and there is a "feature pack" as well... ...
  • SQL database mirroring
    I have SQL Server 2008 R2 Enterprise 64bit on Windows 2008 R2 Enterprise 64bit.  Each SQL Server has...
  • Dell Compellent Disk Drive
    Does anybody has experience with Dell Compellent Disk Drive? Basically, this system manages all disk...
  • Sql server performance tuning
    I need to find a tool that help me to optimize sql server,queries,improve the performance and solve ...