Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Merge statement

Posted on 2013-11-03
3
Medium Priority
?
245 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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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
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

876 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