Solved

Merge statement

Posted on 2013-11-03
3
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

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.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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 shrink a transaction log file down to a reasonable size.

623 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