ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 深入SQL之merge into

深入SQL之merge into

原创 Linux操作系统 作者:wmlm 时间:2008-05-09 14:03:44 0 删除 编辑
===========================================================
深入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 )
/

如果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博客 ” ,链接:https://blog.itpub.net/271063/viewspace-263461/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-04-08

  • 博文量
    398
  • 访问量
    3716781