试试mergeOracle9i引入了一个新的SQL语句,使用本语句可以在一条语句中连接两个表。以前要使用UPDATE去更新两个表中都存在的记录,或者用INSERT添加两个合并表中不存在的记录,必须写两个SQL语句,而现在这两种操作都只要一条SQL MERGE语句就可以实现。
create table inventory (part_nointeger,part_count integer);
insert into inventory values(1,5);
insert into inventory values(3,6);create table shipment (part_nointeger,part_count integer);
insert into shipment values(1,2);
insert into shipment values(2,2);MERGE INTO inventory    
USING shipment    
ON (inventory.part_no = shipment.part_no)
WHEN MATCHED THEN   
UPDATE SET part_count = part_count + shipment.part_count
WHEN NOT MATCHED THEN   
INSERT VALUES (shipment.part_no,shipment.part_count);commit;select * from inventory;  
PART_NO   PART_COUNT
1                7      
3                6     
2                2  执行的结果是shipment数据已经被合并到inventory中,所以与inventory中某些东西相匹配的shipment都会被添加到count中,而没有得到的匹配的就不会添加到inventory中。 
在MERGE语句中必须指定一个WHEN MATCHED和一个WHEN NOT MATHCED语句。如果除这两种情况之外还有别的情况,你可能就需要使用一个常规的INSERT或者UPDATE语句。 另外一点是MERGE语句一次只能修改一行记录,而且不能修改在ON子句中引用的列。 MERGE语句的目标表(target table)(在本例中是inventory)必须是一个可以使用INSERT语句进行插入或者UPDATE语句进行更新的表或者视图。源表(source table)(在本例中是shipment)可以是任何的查询表,比如说外部表或者管道化表函数。

解决方案 »

  1.   

    update 表1 set 备注=(select 备注 from 表2 where 表2.证号=表1.证号)
      

  2.   

    我个人就是用"armyyd(不会游泳的猫)"所写的语句,但执行起来出错
    ORA-01427: 单行子查询返回多于一个行
    以前记得用在程序中执行好像没事的,但在SQL*PLUS中执行就出错了.
      

  3.   

    证号一一对应:
    zheng_num  ...   contents
      R004
      R006            zheng_num  ...   contents
       R004            学生正
       R006           已失效update table1 a set a.contents=(select b.contents from table2 b where a.zheng_num=b.zheng_num)
      

  4.   

    如果“证号”唯一的话,armyyd(不会游泳的猫) 和dinya2003(OK) 都应该没错。
      

  5.   

    update t1 set x=(select a from t2 where t1.y between t2.b and t2.c)
    我刚用过,肯定行。
      

  6.   

    但是又一个问题,update table1 a set a.contents=(select b.contents from table2 b where a.zheng_num=b.zheng_num)
    这样会把所有的记录都更新的,假设我只更新b表中又的数据呢!
      

  7.   

    update table a
    set a.contents=(select b.contents from table2 b
                     where a.zheng_num=b.zheng_num)
    where a.zheng_num in(select distinct zheng_num from table2)
                      
    是不是得这样写!
      

  8.   

    肯定是你的证号不唯一
    select count(*) from 表2
    select count(distinct 证号) from 表2
    看两个结果是否相等,就知道你证号是否唯一了
      

  9.   

    加上rownum<2應該可以
    update 表1 set 备注=(select 备注 from 表2 where 表2.证号=表1.证号 and rownum<2)
      

  10.   

    update t1 set 备注=(select 备注 from t2 where 证号=t1.证号)
      

  11.   

    myamor(张俊杰)
    我顶
    已经能够解决问题,但in对性能的影响不知道会不会太大!
      

  12.   

    那就改为exists会快一些。
    update table a
    set a.contents=(select b.contents from table2 b
                     where a.zheng_num=b.zheng_num)
    where exists (select 'X'
                    from table2 c
                   where a.zheng_num = c.zheng_num
                 )
      

  13.   

    update 表1 set 备注=(select distinct 备注 from 表2 where 表2.证号=表1.证号)
      

  14.   

    create table tab1(id varchar2(20),key varchar2(20))create table tab2(id2 varchar2(20),key2 varchar2(20))insert into tab1 values('001','0001')
    insert into tab1 values('002','0002');
    insert into tab1 values('003','0003');
    insert into tab1 values('004','0004');insert into tab2 values('001','00001');
    insert into tab2 values('002','00002');
    insert into tab2 values('003','00003');select * from tab1select * from tab2--错误没有的被更新为null
    update tab1 a
    set key=(select key2 from tab2 b where a.id=b.id2)--正确只有相等才会被更新
    update tab1 a
    set key=(select key2 from tab2 b where a.id=b.id2)
    where exists (select 1 from tab2 b where a.id=b.id2)