Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3843210
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:54:19+00:00 2026-05-19T15:54:19+00:00

I am attempting to use the SQL SERVER 2008 MERGE statement in a stored

  • 0

I am attempting to use the SQL SERVER 2008 MERGE statement in a stored procedure for updating/inserting a table.
I have an INSTEAD OF INSERT trigger on the table, and I get the following error message when attempting to CREATE the procedure

The target ‘Phone’ of the MERGE statement has an INSTEAD OF trigger on some,
but not all, of the actions specified in the MERGE statement. In a MERGE statement, if any action has an enabled INSTEAD OF trigger on the target, then all actions must have enabled INSTEAD OF triggers.

I definitely do not need an INSTEAD OF UPDATE trigger, (and can’t create one because of CASCADE DELETES being enabled on the table).

So in the stored procedure I first issue a DISABLE TRIGGER command before the MERGE. But when I run the stored proc, I get the same error, as if the DISABLE TRIGGER command never gets run.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T15:54:19+00:00Added an answer on May 19, 2026 at 3:54 pm

    The Query Optimizer does a static parse of your T-SQL batch, and as soon as it sees the MERGE statement, it will validate the requirements. It will NOT factor in any DDL statements that affect the triggers before the MERGE statement.

    You can work around this using GO to break the statements into separate batches, but if it is in a single SP (no GO statements), you have two choices

    • put the MERGE into a support SP that the main one calls; or
    • use dynamic SQL

    Dynamic SQL

    Let’s create a table with a trigger

    create table tg1(i int)
    ;
    create trigger tg1_tg on tg1 instead of insert as 
    select 1
    GO
    

    Then attempt to MERGE on the table

    alter table tg1 disable trigger tg1_tg
    ;
    merge tg1 as target
    using (select 1 union all select 3) as source (X) on target.i = source.x
    when matched then
        delete
    when not matched by target then
        insert (i) values (x)
    output $action, inserted.*, deleted.*
    ;
    alter table tg1 enable trigger tg1_tg
    ;
    

    Not good..

    Msg 5316, Level 16, State 1, Line 1
    The target ‘tg1’ of the MERGE statement has an INSTEAD OF trigger on some, but not all, of the actions specified in the MERGE statement. In a MERGE statement, if any action has an enabled INSTEAD OF trigger on the target, then all actions must have enabled INSTEAD OF triggers.

    So we use dynamic SQL

    alter table tg1 disable trigger tg1_tg
    ;
    exec ('
    merge tg1 as target
    using (select 1 union all select 3) as source (X) on target.i = source.x
    when matched then
        delete
    when not matched by target then
        insert (i) values (x)
    output $action, inserted.*, deleted.*
    ;')
    alter table tg1 enable trigger tg1_tg
    ;
    

    Support procedure

    Let’s create a procedure that will perform the MERGE (a production proc probably would have a table variable, use a #temp table or take in some parameters)

    create proc tg1_MERGE as
    merge tg1 as target
    using (select 1 union all select 3) as source (X) on target.i = source.x
    when matched then
        delete
    when not matched by target then
        insert (i) values (x)
    output $action, inserted.*, deleted.*
    ;
    GO
    

    No go…

    Msg 5316, Level 16, State 1, Line 1
    The target ‘tg1’ of the MERGE statement has an INSTEAD OF trigger on some, but not all, of the actions specified in the MERGE statement. In a MERGE statement, if any action has an enabled INSTEAD OF trigger on the target, then all actions must have enabled INSTEAD OF triggers.

    Even to create it, you need to disable the triggers – so disable the trigger and create the proc again – it will work this time around.

    Finally, you can run this batch which works

    alter table tg1 disable trigger tg1_tg
    ;
    exec tg1_MERGE
    ;
    alter table tg1 enable trigger tg1_tg
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to use Filestream in sql server 2008 to store user uploaded
We are attempting to use a SQL Server 2003 database for our test records
i'm running into a strange problem in Microsoft SQL Server 2008. I have a
I'm attempting to use TinyXML to read and save from memory, instead of only
Using SQL Server 2008, I am trying to do something similar to this post
I'm using SQL Server 2008. I should be able to connect to a user-specified
I'm working on a C# application that queries a SQL Server 2008 Express instance
I am attempting to create a linked server from a 2005 to 2008 Microsoft
In attempting to use the Performance Tools on an ASP.NET website I'm getting various
I'm attempting to use Mono to load a bitmap and print it on Linux

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.