Sql Server 2008 中Merge的用法

2022-02-14 07:50:17 字數 3343 閱讀 8158

(本文摘自其它**)sql server 2008中的merge語句能做很多事情,它的功能是根據源表對目標表執行插入、更新或刪除操作。最典型的應用就是進行兩個表的同步。

下面通過乙個簡單示例來演示merge語句的使用方法,假設資料庫中有兩個表product及productnew,我們的任務是將product的資料同步到productnew(當然同步可能是每天通過job來自動完成的,在此我們只關注merge的使用)。

以下sql建立示例表:

--

源表create

table

product (

productid

varchar(7) not

null

primary

key,

productname

varchar(100) not

null

,

price

decimal(13,2) default0);

insert

into product values ('

4100037

','優盤

',50), ('

4100038

','滑鼠

',30

);--

目標表create

table

productnew (

productid

varchar(7) not

null

primary

key,

productname

varchar(100) not

null

,

price

decimal(13,2) default

0 );

下面再來關注merge語句的基本語法:

merge 目標表

using 源表

on 匹配條件

when matched then

語句when not matched then

語句;以上是merge的最最基本的語法,語句執行時根據匹配條件的結果,如果在目標表中找到匹配記錄則執行when matched then後面的語句,如果沒有找到匹配記錄則執行when not matched then後面的語句。注意源表可以是表,也可以是乙個子查詢語句。

格外強調一點,merge語句最後的分號是不能省略的!

回到我們的示例,顯然product與productnew表的merge匹配條件為主鍵productid欄位,初始情況下,productnew表為空,此時肯定執行的是when not matched then後的語句,我們先只考慮源表遞增的情況,merge語句如下:

merge productnew as

d using

product

ass

on s.productid =

d.productid

when

notmatched

then

insert( productid,productname,price) values(s.productid,s.productname,s.price);

執行後2行受影響,我們已經將product表的資料同步到了productnew表。

現在,我們更新product表4100037產品的**,將其修改為55:

update product set price=55 where productid='4100037';

我們也希望每天同步的時候應該將更新後的**同步到productnew表,顯然此時在merge語句中應該新增when matched then 語句,該語句來更新productnew表的**,新增匹配更新後的merge語句:

merge productnew as

d using

product

ass

on s.productid =

d.productid

when

notmatched

then

insert

( productid,productname,price)

values

(s.productid,s.productname,s.price)

when

matched

then

update

set d.productname = s.productname, d.price = s.price;

執行後2行受影響,為什麼是兩行呢?因為我們的匹配條件只是按productid來關聯的,這樣匹配出來的記錄為2行。另外,我們的update語句裡面沒有更新productid欄位,因為這是完全沒必要的(如果修改了productid欄位會直接走到not matched)。

現在做個破壞,我們將410037產品刪除掉:

delete product where productid='4100037';

明顯,上面給出的merge語句無法同步這種情況,再次回到merge語句的定義,對merge的when not matched then語句稍作擴充套件:

when not matched by target

表示目標表不匹配,by target是預設的,所以上面我們直接使用when not matched then

when not matched by source

表示源表不匹配,即目標表中存在,源表中不存在的情況。

現在我們要完成源表delete後,目標表的同步動作,merge語句如下:

merge 

productnew

asd

using

product

ass

on s.productid =

d.productid

when

not matched by

target

then

insert( productid,productname,price) values

(s.productid,s.productname,s.price)

when

not matched by

source

then

delete

when matched

then

update

set d.productname = s.productname, d.price = s.price;

SqlServer 2008 中Merge的應用

簡介 sqlserver 2008中新增加了merge這個dml關鍵字,msdn對於merge的解釋 根據與源表聯結的結果,對目標表執行insert,update,delete操作.例如 根據目標表與源表的差異,在目標表中執行執行insert,update,delete操作,實現兩個表的同步.語法 ...

SQL Server 2008中SQL增強功能點

在sql server 2008中新增功能,可以使用單個insert命令插入多行 舉例 create table dbo test2 編號 int null,姓名 varchar 20 null,一季度 int null,二季度 int null,三季度 int null,四季度 int null ...

SQL Server 2008中SQL增強功能點

在sql server 2008中新增功能,可以使用單個insert命令插入多行 舉例 create table dbo test2 編號 int null,姓名 varchar 20 null,一季度 int null,二季度 int null,三季度 int null,四季度 int null ...