forked from docs/doc-exports
Reviewed-by: Kacur, Michal <michal.kacur@t-systems.com> Co-authored-by: Yang, Tong <yangtong2@huawei.com> Co-committed-by: Yang, Tong <yangtong2@huawei.com>
115 lines
12 KiB
HTML
115 lines
12 KiB
HTML
<a name="mrs_01_24274"></a><a name="mrs_01_24274"></a>
|
|
|
|
<h1 class="topictitle1">MERGE INTO</h1>
|
|
<div id="body8662426"><div class="section" id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_s56e6e636e92d4e25ba86171d168a0097"><h4 class="sectiontitle">Function</h4><p id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_a36526b302fb741da8affa982ede0120e">This command is used to query another table based on the join condition of a table or subquery. If <strong id="mrs_01_24274__en-us_topic_0000001219029099_b547915405503">UPDATE</strong> or <strong id="mrs_01_24274__en-us_topic_0000001219029099_b14291344135017">DELETE</strong> is executed for the table matching the join condition, and <strong id="mrs_01_24274__en-us_topic_0000001219029099_b105435418517">INSERT</strong> is executed if the join condition is not met. This command completes the synchronization requiring only one full table scan, delivering higher efficiency than <strong id="mrs_01_24274__en-us_topic_0000001219029099_b15478191312560">INSERT</strong> plus <strong id="mrs_01_24274__en-us_topic_0000001219029099_b3947234565">UPDATE</strong>.</p>
|
|
</div>
|
|
<div class="section" id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_sc17acc5cdde24923afe02ad70c5af9a1"><h4 class="sectiontitle">Syntax</h4><p id="mrs_01_24274__en-us_topic_0000001219029099_p096025164317"><strong id="mrs_01_24274__en-us_topic_0000001219029099_b185551752145612">MERGE INTO</strong> <em id="mrs_01_24274__en-us_topic_0000001219029099_i1937320095712">tableIdentifier</em> <em id="mrs_01_24274__en-us_topic_0000001219029099_i10305162410572">AS</em><strong id="mrs_01_24274__en-us_topic_0000001219029099_b188563239574"> </strong><em id="mrs_01_24274__en-us_topic_0000001219029099_i19973765719">target_alias</em></p>
|
|
<p id="mrs_01_24274__en-us_topic_0000001219029099_p10960155144318"><strong id="mrs_01_24274__en-us_topic_0000001219029099_b45501213145710">USING</strong><em id="mrs_01_24274__en-us_topic_0000001219029099_i15439820115716"> (sub_query | tableIdentifier) AS source_alias</em></p>
|
|
<p id="mrs_01_24274__en-us_topic_0000001219029099_p89601564314"><strong id="mrs_01_24274__en-us_topic_0000001219029099_b114131927115718">ON</strong> <em id="mrs_01_24274__en-us_topic_0000001219029099_i1434330135711"><merge_condition></em></p>
|
|
<p id="mrs_01_24274__en-us_topic_0000001219029099_p1696045164318"><em id="mrs_01_24274__en-us_topic_0000001219029099_i25821366575">[ WHEN MATCHED [ AND <condition> ] THEN <matched_action> ]</em></p>
|
|
<p id="mrs_01_24274__en-us_topic_0000001219029099_p1396016514318"><em id="mrs_01_24274__en-us_topic_0000001219029099_i360283615716">[ WHEN MATCHED [ AND <condition> ] THEN <matched_action> ]</em></p>
|
|
<p id="mrs_01_24274__en-us_topic_0000001219029099_p996015154314"><em id="mrs_01_24274__en-us_topic_0000001219029099_i12603153616577">[ WHEN NOT MATCHED [ AND <condition> ] THEN <not_matched_action> ]</em></p>
|
|
<p id="mrs_01_24274__en-us_topic_0000001219029099_p139601352430"></p>
|
|
<p id="mrs_01_24274__en-us_topic_0000001219029099_p10960115204313"><em id="mrs_01_24274__en-us_topic_0000001219029099_i14971165875720"><merge_condition> =A equal bool condition</em></p>
|
|
<p id="mrs_01_24274__en-us_topic_0000001219029099_p296018513430"><em id="mrs_01_24274__en-us_topic_0000001219029099_i79721858145711"><matched_action> =</em></p>
|
|
<p id="mrs_01_24274__en-us_topic_0000001219029099_p99602574316"><em id="mrs_01_24274__en-us_topic_0000001219029099_i119731558155715">DELETE |</em></p>
|
|
<p id="mrs_01_24274__en-us_topic_0000001219029099_p159611514312"><em id="mrs_01_24274__en-us_topic_0000001219029099_i119741258115712">UPDATE SET * |</em></p>
|
|
<p id="mrs_01_24274__en-us_topic_0000001219029099_p12961155174316"><em id="mrs_01_24274__en-us_topic_0000001219029099_i297695810576">UPDATE SET column1 = expression1 [, column2 = expression2 ...]</em></p>
|
|
<p id="mrs_01_24274__en-us_topic_0000001219029099_p6961115144312"><em id="mrs_01_24274__en-us_topic_0000001219029099_i2097655825720"><not_matched_action> =</em></p>
|
|
<p id="mrs_01_24274__en-us_topic_0000001219029099_p159617510436"><em id="mrs_01_24274__en-us_topic_0000001219029099_i6977658105716">INSERT * |</em></p>
|
|
<p id="mrs_01_24274__en-us_topic_0000001219029099_p996125194311"><em id="mrs_01_24274__en-us_topic_0000001219029099_i29781358105720">INSERT (column1 [, column2 ...]) VALUES (value1 [, value2 ...])</em></p>
|
|
</div>
|
|
<div class="section" id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_s237e949a91f645afb133fa8ff1fb3a30"><h4 class="sectiontitle">Parameter Description</h4>
|
|
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_tbf6ea26f1d504556bc00eb71921b92b0" frame="border" border="1" rules="all"><caption><b>Table 1 </b>Parameters</caption><thead align="left"><tr id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_rddbab88c4e884027b074774928deec85"><th align="left" class="cellrowborder" valign="top" width="23.68%" id="mcps1.3.3.2.2.3.1.1"><p id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_abdf5d12b11d4467b97baafd3fcebca8a">Parameter</p>
|
|
</th>
|
|
<th align="left" class="cellrowborder" valign="top" width="76.32%" id="mcps1.3.3.2.2.3.1.2"><p id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_ab3ad580270924283937a10d484199525">Description</p>
|
|
</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody><tr id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_r34842d652b5a49c59aca88bb0efdaaed"><td class="cellrowborder" valign="top" width="23.68%" headers="mcps1.3.3.2.2.3.1.1 "><p id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_a415fa4ab88554922af1ee1285180ce94">tableIdentifier</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="76.32%" headers="mcps1.3.3.2.2.3.1.2 "><p id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_ac9347142ecb343fea4e8122e01b659b5">Name of the Hudi table.</p>
|
|
</td>
|
|
</tr>
|
|
<tr id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_r0166cc86ce3746eaa8c8928e6c025dfa"><td class="cellrowborder" valign="top" width="23.68%" headers="mcps1.3.3.2.2.3.1.1 "><p id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_a85891954f0a74d868c132592dbd9058c">target_alias</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="76.32%" headers="mcps1.3.3.2.2.3.1.2 "><p id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_a03ee028b6d254b31a7e4c55a25c89dd8">Alias of the target table.</p>
|
|
</td>
|
|
</tr>
|
|
<tr id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_r64bff89c5c8a4ae6892cb5a840fffb35"><td class="cellrowborder" valign="top" width="23.68%" headers="mcps1.3.3.2.2.3.1.1 "><p id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_a4510eadd622d4c0a801314f75c488055">sub_query</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="76.32%" headers="mcps1.3.3.2.2.3.1.2 "><p id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_aa6bd0f68abfc4790b37d9f105798cb8c">Subquery.</p>
|
|
</td>
|
|
</tr>
|
|
<tr id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_rb8c1d302640a4adba6c2d6734fc67c64"><td class="cellrowborder" valign="top" width="23.68%" headers="mcps1.3.3.2.2.3.1.1 "><p id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_ab1fb0c8c5f6e43b5b114c95c03c61d33">source_alias</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="76.32%" headers="mcps1.3.3.2.2.3.1.2 "><p id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_ab1c5ec4aea1b4c27a7d82ad2c09dad28">Alias of the source table or source expression.</p>
|
|
</td>
|
|
</tr>
|
|
<tr id="mrs_01_24274__en-us_topic_0000001219029099_row248815115455"><td class="cellrowborder" valign="top" width="23.68%" headers="mcps1.3.3.2.2.3.1.1 "><p id="mrs_01_24274__en-us_topic_0000001219029099_p184881811134518">merge_condition</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="76.32%" headers="mcps1.3.3.2.2.3.1.2 "><p id="mrs_01_24274__en-us_topic_0000001219029099_p2048861194514">Condition for associating the source table or expression with the target table.</p>
|
|
</td>
|
|
</tr>
|
|
<tr id="mrs_01_24274__en-us_topic_0000001219029099_row646992144714"><td class="cellrowborder" valign="top" width="23.68%" headers="mcps1.3.3.2.2.3.1.1 "><p id="mrs_01_24274__en-us_topic_0000001219029099_p34701421184718">condition</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="76.32%" headers="mcps1.3.3.2.2.3.1.2 "><p id="mrs_01_24274__en-us_topic_0000001219029099_p18470162110475">Filtering condition. This parameter is optional.</p>
|
|
</td>
|
|
</tr>
|
|
<tr id="mrs_01_24274__en-us_topic_0000001219029099_row15203182554719"><td class="cellrowborder" valign="top" width="23.68%" headers="mcps1.3.3.2.2.3.1.1 "><p id="mrs_01_24274__en-us_topic_0000001219029099_p12038252473">matched_action</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="76.32%" headers="mcps1.3.3.2.2.3.1.2 "><p id="mrs_01_24274__en-us_topic_0000001219029099_p17203925204713">DELETE or UPDATE operation to be performed when conditions are met.</p>
|
|
</td>
|
|
</tr>
|
|
<tr id="mrs_01_24274__en-us_topic_0000001219029099_row10481152219487"><td class="cellrowborder" valign="top" width="23.68%" headers="mcps1.3.3.2.2.3.1.1 "><p id="mrs_01_24274__en-us_topic_0000001219029099_p20481102244820">not_matched_action</p>
|
|
</td>
|
|
<td class="cellrowborder" valign="top" width="76.32%" headers="mcps1.3.3.2.2.3.1.2 "><p id="mrs_01_24274__en-us_topic_0000001219029099_p348112217489">INSERT operation to be performed when conditions are not met.</p>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792787_sbdb980f99f28440d9181a194ac5f3bea"><h4 class="sectiontitle">Precautions</h4><ol id="mrs_01_24274__en-us_topic_0000001219029099_ol1491864345513"><li id="mrs_01_24274__en-us_topic_0000001219029099_li1991812435551">The merge-on condition supports only primary key columns currently.</li><li id="mrs_01_24274__en-us_topic_0000001219029099_li1918154315551">Currently, only some fields in COW tables can be updated. The following is an example:<pre class="screen" id="mrs_01_24274__en-us_topic_0000001219029099_screen2902866560">merge into h0 using s0
|
|
on h0.id = s0.id
|
|
when matched then update set price = s0.price * 2</pre>
|
|
</li><li id="mrs_01_24274__en-us_topic_0000001219029099_li1495512564558">Currently, only when COW tables are updated, fields in the target table can appear in the right of the UPDATE expression. The following is an example:<pre class="screen" id="mrs_01_24274__en-us_topic_0000001219029099_screen12513911155619">merge into h0 using s0
|
|
on h0.id = s0.id
|
|
when matched then update set id = s0.id,
|
|
name = <strong id="mrs_01_24274__en-us_topic_0000001219029099_b1731352820919">h0.name</strong>,
|
|
price = s0.price + <strong id="mrs_01_24274__en-us_topic_0000001219029099_b041024512910">h0.price</strong></pre>
|
|
</li></ol>
|
|
</div>
|
|
<div class="section" id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_sb848b6b6f29c4ca89355bd1310c46e8b"><h4 class="sectiontitle">Examples</h4><pre class="screen" id="mrs_01_24274__en-us_topic_0000001219029099_screen66273525616">merge into h0 as target
|
|
using (
|
|
select id, name, price, flag from s
|
|
) source
|
|
on target.id = source.id
|
|
when matched then update set *
|
|
when not matched then insert *;
|
|
|
|
merge into h0
|
|
using (
|
|
select id, name, price, flag from s
|
|
) source
|
|
on h0.id = source.id
|
|
when matched and flag != 'delete' then update set id = source.id, name = source.name, price = source.price * 2
|
|
when matched and flag = 'delete' then delete
|
|
when not matched then insert (id,name,price) values(source.id, source.name, source.price);
|
|
|
|
merge into t0 as target
|
|
using s0 source
|
|
on target.id = source.id
|
|
when matched then update set *
|
|
when not matched then insert *;</pre>
|
|
</div>
|
|
<div class="section" id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_s49b19580d9be445b92aafa75822ad7a2"><h4 class="sectiontitle">System Response</h4><p id="mrs_01_24274__en-us_topic_0000001219029099_en-us_topic_0265792798_a14f5dd2a5132480991d1f4c428544d79">You can view the result in driver logs or on the client.</p>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="mrs_01_24272.html">Hudi DML</a></div>
|
|
</div>
|
|
</div>
|
|
|