Solved

Merge Statment

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

713 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