Solved

Merge statement

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

911 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

22 Experts available now in Live!

Get 1:1 Help Now