惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
博客园 - 聂微东
Application and Cybersecurity Blog
Application and Cybersecurity Blog
MyScale Blog
MyScale Blog
罗磊的独立博客
H
Help Net Security
L
LangChain Blog
T
Threat Research - Cisco Blogs
量子位
S
Securelist
Last Week in AI
Last Week in AI
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
The Hacker News
The Hacker News
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Blog of Author Tim Ferriss
T
Threatpost
Security Latest
Security Latest
P
Palo Alto Networks Blog
Microsoft Security Blog
Microsoft Security Blog
NISL@THU
NISL@THU
F
Full Disclosure
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Heimdal Security Blog
J
Java Code Geeks
Recorded Future
Recorded Future
Hugging Face - Blog
Hugging Face - Blog
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
B
Blog RSS Feed
月光博客
月光博客
C
Cisco Blogs
V
Visual Studio Blog
D
DataBreaches.Net
H
Hacker News: Front Page
博客园 - 叶小钗
N
News and Events Feed by Topic
爱范儿
爱范儿
A
Arctic Wolf

博客园 - 无意

程序猿的故事 余额宝收益查询_最新收益率 外国名著小说大全 linux 查看系统版本 32位 or 64位 redhat linux websphere 6 命令行安装(静默安装) 名著小说在线阅读 历史天气查询 oracle分区表之交换分区 altertable exchange partition with table 新工作 oracle 日常维护工作内容 RedHat Linux常见命令 Oracle to_char格式化函数 ROLLUP和CUBE语句 Oracle分析函数参考手册 oracle游标 oracle 存储过程模板 11gr2中DBA_TAB_MODIFICATIONS视图返回结果异常 设置AUTOTRACE时出现SP2-0611错误 利用dbms_system包SET_SQL_TRACE_IN_SESSION开启sql跟踪
Oracle Merge into 详细介绍
无意 · 2010-05-27 · via 博客园 - 无意

/*Merge into 详细介绍
MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句。
通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询,
连接条件匹配上的进行UPDATE,无法匹配的执行INSERT。
这个语法仅需要一次全表扫描就完成了全部工作,执行效率要高于INSERT+UPDATE。
*/
/*語法:
MERGE [INTO [schema .] table [t_alias]
USING [schema .] { table | view | subquery } [t_alias]
ON ( condition )
WHEN MATCHED THEN merge_update_clause
WHEN NOT MATCHED THEN merge_insert_clause;
*/

语法:

MERGE INTO [your table-name] [rename your table here]

USING ( [write your query here] )[rename your query-sql and using just like a table]

ON ([conditional expression here] AND [...]...)

WHEN MATHED THEN [here you can execute some update sql or something else ]

WHEN NOT MATHED THEN [execute something else here ! ]


/*
我们还是以《sql中的case应用》中的表为例。在创建另两个表fzq1和fzq2
*/
--全部男生记录
create table fzq1 as select * from fzq where sex=1;
--全部女生记录
create table fzq2 as select * from fzq where sex=0;
/*涉及到两个表关联的例子*/
--更新表fzq1使得id相同的记录中chengji字段+1,并且更新name字段。
--如果id不相同,则插入到表fzq1中.
--将fzq1表中男生记录的成绩+1,女生插入到表fzq1中
merge into fzq1  aa     --fzq1表是需要更新的表
using fzq bb            -- 关联表
on (aa.id=bb.id)        --关联条件
when matched then       --匹配关联条件,作更新处理
update set
aa.chengji=bb.chengji+1,
aa.name=bb.name         --此处只是说明可以同时更新多个字段。
when not matched then    --不匹配关联条件,作插入处理。如果只是作更新,下面的语句可以省略。
insert values( bb.id, bb.name, bb.sex,bb.kecheng,bb.chengji);
--可以自行查询fzq1表。
/*涉及到多个表关联的例子,我们以三个表为例,只是作更新处理,不做插入处理。当然也可以只做插入处理*/
--将fzq1表中女生记录的成绩+1,没有直接去sex字段。而是fzq和fzq2关联。
merge into fzq1  aa     --fzq1表是需要更新的表
using (select fzq.id,fzq.chengji
       from fzq join fzq2
       on fzq.id=fzq2.id) bb  -- 数据集
on (aa.id=bb.id)        --关联条件
when matched then       --匹配关联条件,作更新处理
update set
aa.chengji=bb.chengji+1
--可以自行查询fzq1表。
/*不能做的事情*/
merge into fzq1  aa   
using fzq bb          
on (aa.id=bb.id)       
when matched then      
update set
aa.id=bb.id+1
/*系统提示:
ORA-38104: Columns referenced in the ON Clause cannot be updated: "AA"."ID"
我们不能更新on (aa.id=bb.id)关联条件中的字段*/
update fzq1
set  id=(select id+1 from fzq where fzq.id=fzq1.id)
where id in
(select id from fzq)
--使用update就可以更新,如果有更好的方法,谢谢反馈!