Solved

Merge Statment

Posted on 2013-12-13
2
199 Views
Last Modified: 2013-12-15
Hi

I have merge statement like below, for eq. I have 1000 rows in t1 target , 900 rows in t2 the source and all of them in t1 target, I found DateModified has a new date for 900, so far is perfect.
But OUTPUT $action gives me 1000 for Update, why?, it should be 900 .
My script like this

Merge t1 t
Using (
select
* from t2
) As s
ON   s.ID=t.ID  
WHEN MATCHED THEN UPDATE
SET

 t.col1=s.col1,
 t.col2=s.col2,
t.DateModified=GetDate()


WHEN NOT MATCHED BY TARGET THEN  
Insert
(
t.col1,
 t.col2,
t.DateModified
)
Values (
s.col1,
 s.col2,
GetDate()
         )
WHEN NOT MATCHED BY Source THEN UPDATE
SET t.IsActive=0
 OUTPUT $action into @Actions;
0
Comment
Question by:SanPrg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
Angelp1ay earned 500 total points
ID: 39718353
For the 900 that match you're updating from source to target and for the 100 that aren't in the source you're setting t.IsActive=0.
0
 

Author Closing Comment

by:SanPrg
ID: 39720553
Aha, Thanks.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question