深入SQL之merge into
===========================================================
深入SQL之merge into
===========================================================
在SQL語句中使用merge來實現插入與更新的合併操作,針對於從一個表插入或更新到另一個表有用
-- merge 用法
merge into sess_stats
using
(
select a.name, b.value
from v$statname a, v$sesstat b
where a.statistic# = b.statistic#
and b.sid = :sid
and (a.name like '%ga %'
or a.name like '%direct temp%')
) curr_stats
on (sess_stats.name = curr_stats.name)
when matched then
update set diff = curr_stats.value - sess_stats.value,
value = curr_stats.value
when not matched then
insert ( name, value, diff )
values
( curr_stats.name, curr_stats.value, null )
/
-- merge 用法
merge into sess_stats
using
(
select a.name, b.value
from v$statname a, v$sesstat b
where a.statistic# = b.statistic#
and b.sid = :sid
and (a.name like '%ga %'
or a.name like '%direct temp%')
) curr_stats
on (sess_stats.name = curr_stats.name)
when matched then
update set diff = curr_stats.value - sess_stats.value,
value = curr_stats.value
when not matched then
insert ( name, value, diff )
values
( curr_stats.name, curr_stats.value, null )
/
如果sess_stats、 curr_stats都是組合主鍵,即還有一個欄位add也是主鍵之一,那麼如下語句又該如何編寫呢?
on (sess_stats.name = curr_stats.name)
試圖改成:
on (sess_stats.name = curr_stats.name,sess_stats.add = curr_stats.add)
on (sess_stats.name = curr_stats.name and sess_stats.add = curr_stats.add)
lhpgh 評論於:2008.02.29 17:07
引文來源 王旺的書房 | 深入SQL之merge into
Link URL: http://wworacle.blog.163.com/blog/static/21268725200849114913369
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/271063/viewspace-263461/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- sql merge intoSQL
- SQL中Merge的用法SQL
- SQL 高階語法 MERGE INTOSQL
- SQL Server之深入理解STUFFSQLServer
- merge sql error, dbType sqlserver, druid1.1.10, sql : nullSQLErrorServerUINull
- ORACLE sql merge into update where條件位置與效能消耗OracleSQL
- 簡單練習Microsoft SQL Server MERGE同步兩個表ROSSQLServer
- SQL Server的Merge —— 一步實現 insert,update,deleteSQLServerdelete
- Java 8 中 Map 騷操作之 merge() 的用法Java
- 排序演算法之「歸併排序(Merge Sort)」排序演算法
- MySQL 優化之 index_merge (索引合併)MySql優化Index索引
- Merge Or Rebase
- git mergeGit
- mysql實現merge功能之DUPLICATE key UPDATE語法MySql
- ou have not concluded your merge (MERGE_HEAD exists)
- 深入瞭解 TiDB SQL 優化器TiDBSQL優化
- SQL Server 2008中Analysis Services的新特性——深入SQL Server 2008SQLServer
- git merge origin master git merge origin/master區別GitAST
- git 中止mergeGit
- Polyphase Merge Sort
- SQLServer MERGE 用法SQLServer
- merge into 用法深思
- merge into基本用法
- Sql Server深入的探討鎖機制SQLServer
- SQL語法之SQL 萬用字元SQL字元
- Oracle SQL優化之sql tuning advisorOracleSQL優化
- Sql 之 join 001SQL
- Oracle優化案例-view merge與coe_load_sql_profile固定執行計劃(十五)Oracle優化ViewSQL
- 深入 Nginx 之配置篇Nginx
- 深入前端之replaced element前端
- HTTP深入之快取HTTP快取
- Oracle SQL調優系列之SQL Monitor ReportOracleSQL
- 深入Spring之IOC之載入BeanDefinitionSpringBean
- git merge失敗Git
- Java HashMap merge() 方法JavaHashMap
- Merge Two Sorted List
- 56. Merge Intervals
- Merge語法限制
- 測試merge效率