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

  • SEARCH
  • Home
  • 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 8280753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:47:44+00:00 2026-06-08T09:47:44+00:00

I have a target table containing a items that have an IsActive flag, and

  • 0

I have a target table containing a items that have an IsActive flag, and I am inserting and updating from a source table using a MERGE statement. If something exists in the source table then it’s active, and if it doesn’t then it’s not active. The logic is pretty simple:

  • if it exists in the source and the target the row should have IsActive true
  • if it exists only in the source then a new row should be inserted to the target, with IsActive true
  • if it exists only in the target then IsActive should be set to false.

All very straightforward, except the target table also has a discriminating column SourceId which relates to the source table. So for a given source table, I only want to MERGE against rows with the corresponding SourceId.

(My normalised table contains rows of identical data types from multiple systems – I retrieve the data from those systems individually and thus the need to merge from one source at a time)

Here’s an example:

IF OBJECT_ID('tempdb..#target') IS NOT NULL DROP TABLE #target    
IF OBJECT_ID('tempdb..#source') IS NOT NULL DROP TABLE #source

CREATE TABLE #target  ( Id INT, SourceId INT, IsActive BIT )   
INSERT #target VALUES (1, 1, 0)
INSERT #target VALUES (2, 1, 1)
INSERT #target VALUES (3, 2, 1)

CREATE TABLE #source ( Id INT )    
INSERT #source VALUES (1)
INSERT #source VALUES (4)

DECLARE @SourceId INT = 1;    
SELECT * FROM #target

MERGE INTO #target t
USING
(
    SELECT [Id] FROM #source
) AS s
ON t.[Id] = s.[Id] AND t.[SourceId] = @SourceId
WHEN MATCHED THEN UPDATE SET [IsActive] = 1
WHEN NOT MATCHED BY TARGET THEN INSERT VALUES ([Id], @SourceId, 1)
WHEN NOT MATCHED BY SOURCE THEN UPDATE SET [IsActive] = 0;

SELECT * FROM #target

My initial attempt was to include the AND t.[SourceId] = @SourceId in the merge condition, but obviously that won’t work – it’s restricting the items to merge, but not the target table. The target row ID = 3 won’t match, and so it will be set to inactive, whether or not that additional condition is included.

The end result is that whenever the procedure is run for a source system, all other systems will be set to inactive.

My solution so far is to run the MERGE only for MATCHED and NOT MATCHED BY TARGET, and then run a subsequent UPDATE for the unmatched rows

UPDATE #target
SET [IsEnabled] = 0 
WHERE [SourceId] = @SourceId
AND [ID] NOT IN (SELECT [ID] FROM #source)

Is there any way to include this filter condition in the MERGE statement? Are there any other clever ways to achieve this?

  • 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-06-08T09:47:47+00:00Added an answer on June 8, 2026 at 9:47 am

    So your result set should be

    1 1 1
    2 1 0    
    3 2 1
    4 1 1
    

    in which case your merge statement should be

    merge #target as t
    using #source as source
    on (t.id=source.id)
    when matched then update set isactive=1
    when not matched by target then insert values (id, @sourceid,1)
    when not matched by source and SourceID=@sourceID then update set isactive=0 
    

    Full test:

    CREATE TABLE #target  ( Id INT, SourceId INT, IsActive BIT )    
    INSERT #target VALUES (1, 1, 0) 
    INSERT #target VALUES (2, 1, 1) 
    INSERT #target VALUES (3, 2, 1) 
    
    CREATE TABLE #source ( Id INT )     
    INSERT #source VALUES (1) 
    INSERT #source VALUES (4) 
    
    DECLARE @SourceId INT 
    select @SourceId = 1;     
    
    merge #target as t 
    using #source as source 
    on (t.id=source.id) 
    when matched then update set isactive=1 
    when not matched by target then insert values (id, @sourceid,1) 
    when not matched by source and SourceID=@SourceID then update set isactive=0;
    
    
    SELECT * FROM #target 
    
    drop table #target;
    drop table #source
    

    results…

    Id          SourceId    IsActive
    ----------- ----------- --------
    1           1           1
    2           1           0
    3           2           1
    4           1           1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a case of updating the target table where source columns data not
I have a postgresql table containing movements of different items (models) between warehouses. For
I have to delete rows from guide_category that have no relation with guide table
I have two separate table, I want to merge into a single table using
Very simplified, I have two tables Source and Target. declare @Source table (SourceID int
I have two tables: a source table and a target table. The target table
I have a source table that is NewID|Fruit|Apples and I need to insert those
I have a target table TargetTable that has a DECIMAL(20, 7) column. I also
I have this bit of JavaScript... 15 $('.ajax_edit_address').each(function() { 16 $(this).ajaxForm({ 17 target: $(this).parents('table.address').find('tr.address_header').children(':first'),
I have an ant target that echo's the content of an eclipse .project file,

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.