Solved

Merge Statment

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL Bulk load data error 5 34
ssms - object execution statistics 12 37
Need Counts 11 43
SQL server 2008 SP4 29 34
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now