Solved

Merge statement

Posted on 2013-11-03
3
236 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

759 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

21 Experts available now in Live!

Get 1:1 Help Now