Solved

Merge statement

Posted on 2013-11-03
3
239 Views
Last Modified: 2013-11-04
Hi ,
I am trying to use Merge statement rather than Insert and Update.

I need to know how many rows are inserted and updated.
is there  any way to know which column's value is changed if I have many columns to updated?
Thanks
0
Comment
Question by:SanPrg
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 500 total points
ID: 39620805
Use
 OUTPUT $action INTO #SummaryOfChanges;

Open in new window

with merge statement

Demonstration Code


CREATE TABLE #StudentDetails
 (
 StudentID INTEGER PRIMARY KEY,
 StudentName VARCHAR(15)
 )
 GO

  INSERT INTO #StudentDetails
 VALUES(1,'SMITH')
 INSERT INTO #StudentDetails
 VALUES(2,'ALLEN')
 INSERT INTO #StudentDetails
 VALUES(3,'JONES')
 INSERT INTO #StudentDetails
 VALUES(4,'MARTIN')
 INSERT INTO #StudentDetails
 VALUES(5,'JAMES')
 GO

 CREATE TABLE #StudentTotalMarks
 (
 StudentID INTEGER ,
 StudentMarks INTEGER
 )
 GO

 INSERT INTO #StudentTotalMarks
 VALUES(1,230)
 INSERT INTO #StudentTotalMarks
 VALUES(2,255)
 INSERT INTO #StudentTotalMarks
 VALUES(3,200)
 GO

 -- Select from Table
 SELECT *
 FROM #StudentDetails
 GO
 SELECT *
 FROM #StudentTotalMarks
 GO
  
  create table #SummaryOfChanges (Change VARCHAR(20));

 -- Merge Statement
 MERGE #StudentTotalMarks AS stm
 USING (SELECT StudentID,StudentName FROM #StudentDetails) AS sd
 ON stm.StudentID = sd.StudentID
 WHEN MATCHED AND stm.StudentMarks > 250 THEN DELETE
 WHEN MATCHED THEN UPDATE SET stm.StudentMarks = stm.StudentMarks + 25
 WHEN NOT MATCHED THEN
 INSERT(StudentID,StudentMarks)
 VALUES(sd.StudentID,25)
 OUTPUT $action INTO #SummaryOfChanges;
 GO
 

SELECT Change, COUNT(*) AS CountPerChange
FROM #SummaryOfChanges
GROUP BY Change;

 -- Clean up
 DROP TABLE #StudentDetails
 GO
 DROP TABLE #StudentTotalMarks
 GO

Open in new window


Courtesy :-
http://blog.sqlauthority.com/2010/06/08/sql-server-merge-operations-insert-update-delete-in-single-execution/
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/c06e1db4-7fd6-43c4-8569-5335d555dac8/record-count-on-merge-statement-on-insertupdatedelete?forum=transactsql

Raj
0
 

Author Closing Comment

by:SanPrg
ID: 39621848
Thanks
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 39623317
you are welcome
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

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…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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

815 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

12 Experts available now in Live!

Get 1:1 Help Now