Troubleshooting Guide: Error 5315 – “The Target of a MERGE Statement Cannot Be a Remote Table”
Why This Error Happens
This error occurs because SQL Server’s MERGE statement does not support referencing a remote table (via a linked server) as the target of the operation. SQL Server 2025 enforces strict transactional and locking guarantees for MERGE operations that cannot be reliably coordinated across a linked server connection. This limitation is architectural, not a bug, and typically surfaces when developers attempt cross-server data synchronization using MERGE instead of standard INSERT/UPDATE/DELETE logic.
Step-by-Step Resolution Guide
Step 1: Identify the Linked Server Reference in the MERGE Statement
- Review the MERGE statement’s target clause (the table immediately following the
MERGE INTOkeyword). - Check if the target table name is qualified with a four-part name (e.g.,
LinkedServerName.DatabaseName.Schema.TableName). - Confirm the linked server by running
SELECT * FROM sys.servers WHERE is_linked = 1;in SQL Server Management Studio (SSMS) 21.
Step 2: Restructure the Query to Use a Local Staging Table
- Create a local temporary table or table variable on the server where the MERGE will execute.
- Populate it with data pulled from the remote server first:
SELECT * INTO #StagingTable FROM LinkedServerName.DB.Schema.SourceTable; - Rewrite the MERGE statement so both the source and target reference only local objects, using
#StagingTableas the source instead of the remote reference.
Step 3: Push Execution to the Remote Server via Stored Procedure
- If the target table must remain on the remote instance, use
sp_executesqlorOPENQUERYto execute the MERGE statement remotely instead of locally referencing it. - Example: Wrap the MERGE logic inside a stored procedure on the remote server, then call it using
EXEC LinkedServerName.DatabaseName.Schema.ProcedureName;from the local instance. - Validate linked server permissions in SSMS > Server Objects > Linked Servers > Security to ensure the calling login has
EXECUTErights on the remote procedure.
Step 4: Verify Linked Server Configuration and Provider Settings
- Open SQL Server Configuration Manager on Windows Server 2025 and confirm the OLE DB provider used for the linked server supports distributed transactions.
- In SSMS, right-click the Linked Server, select Properties, and verify “RPC” and “RPC Out” are set to
Trueunder Server Options if remote procedure calls are required. - Restart the SQL Server service (
services.mscornet stop MSSQLSERVER && net start MSSQLSERVER) after changing provider options.
Step 5: Enable MSDTC if Using Distributed Transactions
- Open Component Services (
dcomcnfg.exe) on both the local and remote Windows Server 2025 instances. - Navigate to Component Services > Computers > My Computer > Distributed Transaction Coordinator and ensure the MSDTC service is running.
- Enable “Network DTC Access” under Security Configuration and restart the MSDTC service for changes to take effect.
Alternative or Verification Steps
- Run the rewritten MERGE statement (using the local staging table approach) and confirm it executes without Error 5315 in the SSMS Messages pane.
- As a quick alternative, replace the MERGE logic entirely with separate
INSERT INTO ... SELECT,UPDATE, andDELETEstatements targeting the linked server directly, since standard DML statements do not share the same remote-target restriction as MERGE.