How to properly trigger an insert to a linked sql server?
Perform inserts from a trigger into a table on linked server - bad decision.
This will great affects on insert performance in the source table ([dbo].[Table_1])
and also there is a distributed transaction, and configuring servers
for support distributed transactions - nightmare.
One possible solution is:
On the source server you can create a synchronization queue table. For example:
CREATE TABLE dbo.SyncQueue
(
QueueId INT IDENTITY(1,1),
KeyForSync INT, -- Primary key value of record in dbo.SourceTable
SyncStatus INT -- statuses can be: 0 - New, 1 - Synchronized, 2 - Error
)
suppose you source table is
CREATE TABLE dbo.SourceTable
(
Key INT, -- primary key of the table
Data varchar(xxx)
)Triger on dbo.SourceTable can quickly insert into dbo.SyncQueue record Key that you need synchronize
- Some periodically performed stored procedure can then insert records from the queue in
table on the linked server.
INSERT trigger from another table from a linked Server is slow
Triggers should always be written to be as fast as possible to avoid exactly this problem. Intensive operations of any kind should ideally not be carried out within a trigger. That applies doubly when the operation involves contacting other servers as that can easily end up taking real time.
Instead queue the action and process your queue outside the trigger using a service or agent.
A queue might looks like a record in a table which gets flagged once processed. The record needs to contain enough information for the service to carry out the related actions, which could be contained within a Stored Procedure.
Related Topics
Set Empty Strings ('') to Null in the Whole Database
What Are the Conditions for Encountering a Serialization Failure
Openrowset for Excel: How to Skip Several Rows
SQL Query Aggregate May Not Appear in Where Clause
How to Subtract 2 Dates in Oracle to Get the Result in Hour and Minute
Search All Databases for Value
SQL Question: Does the Order of the Where Clause Make a Difference
Removing Duplicates from a SQL Query (Not Just "Use Distinct")
Enforce a Foreign-Key Constraint to Columns of Same Table
Oracle Insert Failure:Not a Valid Month
SQL - Should I Use a Junction Table or Not
Does SQLite Support Scope_Identity
Insert Manually into a Table by SQL Statement, But Key Is Autoincremented
Best Practices for the Order of Joined Columns in a SQL Join