MERGE 語句示例說明

2021-05-21 21:15:49 字數 4364 閱讀 6215

ibm informix ids merge語句為dml語句,用來對比兩個表的記錄情況,根據情況執行insert的同時執行update或者delete操作。

ids11.5xc6及以上版本支援該功能

1.表sale為目標表,new_sale為**表,通過cust_id對比兩個表的資料,當sale表有與new_sale相同的記錄時,執行update更新sale表的salecount屬性,當sale表中沒有new_sale中相應的記錄,則把該記錄insert 到sale表中

merge into sale using new_sale as n

on sale.cust_id = n.cust_id

when matched then update

set sale.salecount = sale.salecount + n.salecount

when not matched then insert (cust_id, salecount)

values (n.cust_id, n.salecount);

2.merge into customer c

using ext_customer e

on c.customer_num=e.customer_num

when matched then

update set c.fname = e.fname,

c.lname = e.lname,

c.company = e.company,

c.address1 = e.address1,

c.address2 = e.address2,

c.city = e.city,

c.state = e.state,

c.zipcode = e.zipcode,

c.phone = e.phone

when not matched then

insert (c.fname, c.lname, c.company, c.address1, c.address2,

c.city, c.state, c.zipcode, c.phone)

values

(e.fname, e.lname, e.company, e.address1, e.address2,

e.city, e.state, e.zipcode, e.phone);

3.merge into customer c

using ext_customer e

on c.customer_num=e.customer_num

and c.fname=e.fname and c.lname=e.lname

when matched then

update set c.fname = e.fname,

c.lname = e.lname,

c.company = e.company,

c.address1 = e.address1,

c.address2 = e.address2,

c.city = e.city,

c.state = e.state,

c.zipcode = e.zipcode,

c.phone = e.phone

when not matched then

insert

(c.fname, c.lname, c.company, c.address1, c.address2,

c.city, c.state, c.zipcode, c.phone)

values

(e.fname, e.lname, e.company, e.address1, e.address2,

e.city, e.state, e.zipcode, e.phone);

4.merge into customer c

using ext_customer e

on c.customer_num=e.customer_num

when matched then

update set c.fname = e.fname,

c.lname = e.lname,

c.company = e.company,

c.address1 = e.address1,

c.address2 = e.address2,

c.city = e.city,

c.state = e.state,

c.zipcode = e.zipcode,

c.phone = e.phone ;

5.merge into customer c

using ext_customer e

on c.customer_num=e.customer_num

when matched then

delete ;

6.merge into customer c

using ext_customer e

on c.customer_num=e.customer_num and c.fname=e.fname

and c.lname=e.lname

when not matched then

insert

(c.fname, c.lname, c.company, c.address1, c.address2,

c.city, c.state, c.zipcode, c.phone)

values

(e.fname, e.lname, e.company, e.address1, e.address2,

e.city, e.state, e.zipcode, e.phone);

7.merge into customer c

using ext_customer e

on c.customer_num=e.customer_num and c.fname=e.fname and c.lname=e.lname

when not matched then

insert

(c.fname, c.lname, c.company, c.address1, c.address2,

c.city, c.state, c.zipcode, c.phone)

values

(e.fname, e.lname, e.company, e.address1, e.address2,

e.city, e.state, e.zipcode, e.phone)

when matched then update

set c.fname = e.fname,

c.lname = e.lname,

c.company = e.company,

c.address1 = e.address1,

c.address2 = e.address2,

c.city = e.city,

c.state = e.state,

c.zipcode = e.zipcode,

c.phone = e.phone ;

8.merge into customer c

using (select * from ext_customer e1, orders e2

where e1.customer_num=e2.customer_num ) e

on c.customer_num=e.customer_num and c.fname=e.fname

and c.lname=e.lname

when not matched then

insert (c.fname, c.lname, c.company, c.address1, c.address2,

c.city, c.state, c.zipcode, c.phone)

values (e.fname, e.lname, e.company, e.address1, e.address2,

e.city, e.state, e.zipcode, e.phone)

when matched then

update set c.fname = e.fname,

c.lname = e.lname,

c.company = e.company,

c.address1 = e.address1,

c.address2 = e.address2,

c.city = e.city,

c.state = e.state,

c.zipcode = e.zipcode,

c.phone = e.phone ;

MERGE語句的使用!

今天需要更新乙個表的標識,需要和另外乙個表關聯,但是update無法做到這點。a表中有乙個memo,b表中有乙個欄位存了幾個string,當b表中的任何乙個string為a表中的memo的子串時,更新a表中flag。update如下 update passap.tb finpay vth check...

Merge 語句的使用

db2 merge 語句的作用非常強大,它可以將乙個表中的資料合併到另乙個表中,在合併的同時可以進行插入 刪除 更新等操作。我們還是先來看個簡單的例子吧,假設你定義了乙個雇員表 employe 乙個經理表 manager 如下所示 雇員表 employe create table employe e...

MERGE語句的使用 複製表

今天需要更新乙個表的標識,需要和另外乙個表關聯,但是update無法做到這點。a表中有乙個memo,b表中有乙個欄位存了幾個string,當b表中的任何乙個string為a表中的memo的子串時,更新a表中flag。update如下 update passap.tb finpay vth check...