Oracle merge into 用法详解
2021/9/16 19:38:14
本文主要是介绍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">>=</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="{"spm":"1001.2101.3001.4259"}"></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="{"spm":"1001.2101.3001.4259"}"></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="{"spm":"1001.2101.3001.4259"}"></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="{"spm":"1001.2101.3001.4259"}"></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.www.zyiz.net/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="{"spm":"1001.2101.3001.4259"}"></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>
这篇关于Oracle merge into 用法详解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23增量更新怎么做?-icode9专业技术文章分享
- 2024-11-23压缩包加密方案有哪些?-icode9专业技术文章分享
- 2024-11-23用shell怎么写一个开机时自动同步远程仓库的代码?-icode9专业技术文章分享
- 2024-11-23webman可以同步自己的仓库吗?-icode9专业技术文章分享
- 2024-11-23在 Webman 中怎么判断是否有某命令进程正在运行?-icode9专业技术文章分享
- 2024-11-23如何重置new Swiper?-icode9专业技术文章分享
- 2024-11-23oss直传有什么好处?-icode9专业技术文章分享
- 2024-11-23如何将oss直传封装成一个组件在其他页面调用时都可以使用?-icode9专业技术文章分享
- 2024-11-23怎么使用laravel 11在代码里获取路由列表?-icode9专业技术文章分享
- 2024-11-22怎么实现ansible playbook 备份代码中命名包含时间戳功能?-icode9专业技术文章分享