Solved

Merge Statment

Posted on 2013-12-13
2
197 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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

738 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