Solved

Merge Statment

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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.

770 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