Oracle merge into 用法详解

<div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-1a85854398.css">
                <div id="content_views" class="markdown_views prism-atom-one-dark">
                    <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                        <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                    </svg>
                    <p></p>
<div class="toc">
 <h3><a name="t0"></a>文章目录</h3>
 <ul><li><a href="#1__2" target="_self">1 概述</a></li><li><a href="#2__38" target="_self">2 语法</a></li></ul>
</div>
<p></p> 
<h1><a name="t1"></a><a id="1__2"></a>1 概述</h1> 
<pre class="prettyprint"><code class="prism language-sql has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token number">1.</span> 适用场景:<span class="token string">'有则更新,无则插入'</span>

<span class="token number">2.</span> 好处
   <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> 执行 <span class="token string">'效率高'</span>
   <span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> 语法简洁

<span class="token number">3.</span> 如果不知道 <span class="token keyword">merge</span> <span class="token keyword">into</span> 这个语法,咱可能会这么写
   <span class="token keyword">select</span> <span class="token function">count</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
     <span class="token keyword">into</span> v_count
     <span class="token keyword">from</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">;</span>
    
   <span class="token keyword">if</span> <span class="token function">count</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">&gt;=</span> <span class="token number">1</span> <span class="token keyword">then</span>
      <span class="token keyword">update</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">;</span> <span class="token comment">-- 有则更新</span>
   <span class="token keyword">else</span>
      <span class="token keyword">insert</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">;</span> <span class="token comment">-- 无则插入</span>
<div class="hljs-button {2}" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.4259&quot;}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li></ul></pre> 
<p><strong>基础数据准备:</strong></p> 
<pre class="prettyprint"><code class="prism language-sql has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">create</span> <span class="token keyword">table</span> source_table <span class="token punctuation">(</span>
  sno   number<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  sname varchar2<span class="token punctuation">(</span><span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  sex   varchar2<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">insert</span> <span class="token keyword">into</span> source_table<span class="token punctuation">(</span>sno<span class="token punctuation">,</span> sname<span class="token punctuation">,</span> sex<span class="token punctuation">)</span> <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'瑶瑶'</span><span class="token punctuation">,</span> <span class="token string">'女'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> source_table<span class="token punctuation">(</span>sno<span class="token punctuation">,</span> sname<span class="token punctuation">,</span> sex<span class="token punctuation">)</span> <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'优优'</span><span class="token punctuation">,</span> <span class="token string">'男'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">insert</span> <span class="token keyword">into</span> source_table<span class="token punctuation">(</span>sno<span class="token punctuation">,</span> sname<span class="token punctuation">,</span> sex<span class="token punctuation">)</span> <span class="token keyword">values</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token string">'倩倩'</span><span class="token punctuation">,</span> <span class="token string">'女'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">commit</span><span class="token punctuation">;</span>

<span class="token comment">-- 目标表(表结构)</span>
<span class="token keyword">create</span> <span class="token keyword">table</span> target_table <span class="token keyword">as</span> <span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> source_table <span class="token keyword">where</span> <span class="token number">1</span> <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>
<div class="hljs-button {2}" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.4259&quot;}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li></ul></pre> 
<h1><a name="t2"></a><a id="2__38"></a>2 语法</h1> 
<pre class="prettyprint"><code class="prism language-sql has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">merge</span> <span class="token keyword">into</span> 目标表 b
<span class="token keyword">using</span> 源表 a
<span class="token keyword">on</span> <span class="token punctuation">(</span>b<span class="token punctuation">.</span>字段<span class="token number">1</span> <span class="token operator">=</span> a<span class="token punctuation">.</span>字段<span class="token number">1</span> <span class="token operator">and</span> b<span class="token punctuation">.</span>字段n <span class="token operator">=</span> a<span class="token punctuation">.</span>字段n<span class="token punctuation">)</span> <span class="token comment">-- 必须带 '()'</span>
<span class="token keyword">when</span> <span class="token keyword">matched</span> <span class="token keyword">then</span> <span class="token comment">-- 整体扫描,匹配时,执行此处</span>
   <span class="token keyword">update</span> 子句
<span class="token keyword">when</span> <span class="token operator">not</span> <span class="token keyword">matched</span> <span class="token keyword">then</span> <span class="token comment">-- 整体扫描,不匹配时,执行此处</span>
   <span class="token keyword">insert</span> 子句<span class="token punctuation">;</span>
<div class="hljs-button {2}" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.4259&quot;}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li></ul></pre> 
<p><strong>实例: 将源表 source_table 的数据同步至目标表 target_table</strong></p> 
<pre class="prettyprint"><code class="prism language-sql has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">merge</span> <span class="token keyword">into</span> target_table b
<span class="token keyword">using</span> source_table a
<span class="token keyword">on</span> <span class="token punctuation">(</span>b<span class="token punctuation">.</span>sno <span class="token operator">=</span> a<span class="token punctuation">.</span>sno<span class="token punctuation">)</span>
<span class="token keyword">when</span> <span class="token keyword">matched</span> <span class="token keyword">then</span>
  <span class="token keyword">update</span> <span class="token keyword">set</span> b<span class="token punctuation">.</span>sname <span class="token operator">=</span> a<span class="token punctuation">.</span>sname<span class="token punctuation">,</span> b<span class="token punctuation">.</span>sex <span class="token operator">=</span> a<span class="token punctuation">.</span>sex
<span class="token keyword">when</span> <span class="token operator">not</span> <span class="token keyword">matched</span> <span class="token keyword">then</span>
  <span class="token keyword">insert</span> <span class="token punctuation">(</span>b<span class="token punctuation">.</span>sno<span class="token punctuation">,</span> b<span class="token punctuation">.</span>sname<span class="token punctuation">,</span> b<span class="token punctuation">.</span>sex<span class="token punctuation">)</span> <span class="token keyword">values</span> <span class="token punctuation">(</span>a<span class="token punctuation">.</span>sno<span class="token punctuation">,</span> a<span class="token punctuation">.</span>sname<span class="token punctuation">,</span> a<span class="token punctuation">.</span>sex<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">-- commit; -- 记得提交哦</span>
<div class="hljs-button {2}" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.4259&quot;}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li></ul></pre> 
<p><strong>查询结果:</strong><br> <img src="https://www.icode9.com/i/ll/?i=20201113213058442.png#pic_left" alt="在这里插入图片描述"></p> 
<p><strong>提示:咱也可以改变源表 source_table 的记录,测试同步后目标表 target_table 的变化哦</strong></p> 
<pre class="prettyprint"><code class="prism language-sql has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">update</span> source_table t <span class="token keyword">set</span> t<span class="token punctuation">.</span>sname <span class="token operator">=</span> <span class="token string">'aa'</span> <span class="token keyword">where</span> t<span class="token punctuation">.</span>sno <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token keyword">commit</span><span class="token punctuation">;</span>
<div class="hljs-button {2}" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.4259&quot;}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li></ul></pre>
                </div><div><div></div></div>
                <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/markdown_views-d7a94ec6ab.css" rel="stylesheet">
                <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/style-49037e4d27.css" rel="stylesheet">
        </div>

上一篇:1085 PAT单位排行 (25 point(s)) (测试点五)


下一篇:关于还没毕业的大四新手,学习Java的每日分享Day1