功能:根據與源表聯接的結果,對目标表執行插入、更新或删除操作。例如,根據在另一個表中找到的差異在一個表中插入、更新或删除行,可以對兩個表進行同步。
我們看一個例子,假如,有一總産品清單,一個分店産品清單,需要從分店添加産品時更新總産品清單。
總産品表,分店産品表結構完全一緻:

if OBJECT_ID('Demo_AllProducts') is not null
drop table Demo_AllProducts
go
Create table Demo_AllProducts
(PKID int not null identity(1,1) primary key
,DName Nvarchar(20) null
,DCode NVarchar(30) null
,DDate datetime null
)
--this SQL is only for SQL Server 2008
Insert into Demo_AllProducts
(DName,DCode,DDate)
values
('DemoA','AAA',GETDATE()),
('DemoB','BBB',GETDATE()),
('DemoC','CCC',GETDATE()),
('DemoD','DDD',GETDATE()),
('DemoE','EEE',GETDATE())
select * from Demo_AllProducts
--PKID DName DCode DDate
--1 DemoA AAA 2010-10-12 20:33:54.417
--2 DemoB BBB 2010-10-12 20:33:54.417
--3 DemoC CCC 2010-10-12 20:33:54.417
--4 DemoD DDD 2010-10-12 20:33:54.417
--5 DemoE EEE 2010-10-12 20:33:54.417


if OBJECT_ID('Demo_Shop1_Product') is not null
drop table Demo_Shop1_Product
Create table Demo_Shop1_Product
Insert into Demo_Shop1_Product
('DemoB','CCC',GETDATE()),
('DemoF','FFF',GETDATE())
select * from Demo_Shop1_Product
--1 DemoA AAA 2010-10-17 20:19:32.767
--2 DemoB CCC 2010-10-17 20:19:32.767
--3 DemoF FFF 2010-10-17 20:19:32.767

假定現在需要将分店資料完全合并到總産品表中,以編碼字段為依據,如果産品名稱不緻,則用分店的産品名稱替換總産品名稱。
如果總産品表中不存在,則添加。
可選項:如果分店表中不存在,則從總産品表中删除分店中沒有的行。如果這樣,總産品表和分店表就完全同步了。實際操作中可能不需要删除目标表的行。
語句如下:

--确定目标表
Merge Into Demo_AllProducts p
--從資料源查找編碼相同的産品
using Demo_Shop1_Product s on p.DCode=s.DCode
--如果編碼相同,則更新目标表的名稱
When Matched and P.DName<>s.DName Then Update set P.DName=s.DName
--如果目标表中不存在,則從資料源插入目标表
When Not Matched By Target Then Insert (DName,DCode,DDate) values (s.DName,s.DCode,s.DDate)
--如果資料源的行在源表中不存在,則删除源表行
When Not Matched By Source Then Delete;

此時,執行完成後,兩個表的行均如下:
--1 DemoA AAA 2010-10-17 20:31:00.827
--2 DemoB CCC 2010-10-17 20:31:00.827
--3 DemoF FFF 2010-10-17 20:31:00.827
如果不删除,語句如下:

When Not Matched By Target Then Insert (DName,DCode,DDate) values (s.DName,s.DCode,s.DDate);

執行後結果:

--1 DemoA AAA 2010-10-17 20:30:28.350
--2 DemoB BBB 2010-10-17 20:30:28.350
--3 DemoB CCC 2010-10-17 20:30:28.350
--4 DemoD DDD 2010-10-17 20:30:28.350
--5 DemoE EEE 2010-10-17 20:30:28.350
--6 DemoF FFF 2010-10-17 20:31:00.827

如果需要記錄Merge語句影響的行,可以用Output子句,如果僅僅需要知道影響的行數,可以使用@@ROWCOUNT或ROWCOUNT_BIG(),修改後的示例如下:

--定義表變量以存儲輸出
Declare @tableVarRecord Table
(MPKID int not null identity(1,1) primary key
,PKID int null
When Matched and P.DName<>s.DName Then
Update set P.DName=s.DName
When Not Matched By Target Then
Insert (DName,DCode,DDate) values (s.DName,s.DCode,s.DDate)
When Not Matched By Source Then
Delete OUTPUT deleted.* INTO @tableVarRecord;
----Delete OUTPUT Inserted.* INTO @tableVarRecord;
--傳回上個Merge語句影響的行數
select @@ROWCOUNT as Count1,ROWCOUNT_BIG() as Count2
select * from @tableVarRecord;

結果:

--影響的行數
--Count1 Count2
--5 5
--Deleted表的行
--MPKID PKID DName DCode DDate
--1 NULL NULL NULL NULL
--2 2 DemoB BBB 2010-10-17 21:42:30.700
--3 3 DemoC CCC 2010-10-17 21:42:30.700
--4 4 DemoD DDD 2010-10-17 21:42:30.700
--5 5 DemoE EEE 2010-10-17 21:42:30.700

關于@@ROWCOUNT和ROWCOUNT_BIG()的更多說明,請查閱MSDN:
<a href="http://msdn.microsoft.com/en-us/library/ms181406.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/ms181406.aspx</a>
如果影響的結果超過20億,即整型的最大範圍,請使用後者。