dbTalk Databases Forums  

SQL 2005 : AFTER INSERT - Trigger

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss SQL 2005 : AFTER INSERT - Trigger in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Stout
 
Posts: n/a

Default SQL 2005 : AFTER INSERT - Trigger - 06-07-2007 , 01:22 PM






Is it possible to create a trigger in one database, that after an
insert, will update a database on a different server?

If so, how would I do this?

Thanks.

Bill


Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: SQL 2005 : AFTER INSERT - Trigger - 06-07-2007 , 04:25 PM






Stout (stout27 (AT) gmail (DOT) com) writes:
Quote:
Is it possible to create a trigger in one database, that after an
insert, will update a database on a different server?
Yes, this is possible. Whether it is really a good idea is another
matter. Triggers execute in the context of a transaction, and an access
to a linked server may not be that fast. Thus, this can have impact on
concurrency, which depending on the application may matter a lot, or not
at all.

Quote:
If so, how would I do this?
The trigger code as such, is not difficult:

CREATE TRIGGER remote_tri FOR INSERT ON tbl AS
UPDATE SERVER.db.dbo.remotedbl
SET col = i.someval
FROM SERVER.db.dbo.remotedbl r
JOIN inserted i ON r.keycol = i.keycol

The inserted table is a virtual table that holds the newly inserted rows.
(Note plural: a trigger fires once per statement.)

What may be a hassle, is to get everything to work with the distributed
transaction. Keep in mind here that if the update on the remote server
fails, the INSERT must be rolled back. And if local server goes belly-up
before the INSERT is committed, then the operation on the remote server
must also be rolled back. MS DTC handles these low-level parts, but
still some stars have to be aligned. DTC must be running on both servers.
And if the machines run different versions of the operating system
there can be more problems.

One thing you need to ask yourself is how tightly connected the operation
on the remote server has to be on the local server. Maybe something
more loosely coupled like replication is to prefer? Or if you are on
SQL 2005, could Service Broker be a better solution.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.