update 表A set 字段a=(select 字段b from 表B where 表A.ID=表B.id)
where exists (select 1 from 表B where 表A.ID=表B.id);

解决方案 »

  1.   

    to :ORARichard(没钱的日子......) 
    你的意思就是说可以使用语句修改,不必要使用游标了。难道是我的语句有问题吗?在SQL SERVER中
    是没有问题的呀?
      

  2.   

    难道你不希望用语句去更新吗?ORACLE的很多语法都和SQLSERVER不一样
      

  3.   

    update 表A set 字段a (select表B.字段 from b where 表A.ID=表B.id)
      

  4.   

    update 表A set 表A.字段a=(select 表B.字段b from 表B where 表A.ID=表B.id)
    where exists (select 1 from 表B where 表A.ID=表B.id);这样改一下试试,不知道你的错误是什么。
    下次问问题最好把错误提示贴出来,否则别人不好帮你判断。
      

  5.   

    非常抱歉,出差了几天,好几天没上CSDN了。
     TO:  ORARichard(没钱的日子......) 
    还是不行呀?报告的错误为 ORA-01247 单行子查询返回多与一个行。
      

  6.   

    子查询多出一行表示表A.ID=B.ID中,ID不是索引,不是唯一值,换个条件吧。
      

  7.   

    各位大虾,难道 oracle 是不支持语句进行批量修改,只能使用游标吗?
      

  8.   

    update 表A set 表A.字段a=(select max(表B.字段b) from 表B where 表A.ID=表B.id)
    where exists (select 1 from 表B where 表A.ID=表B.id);
      

  9.   

    to: ORARichard(没钱的日子......)
    你这应该是只能修改一条记录吧!
    我是想这么做,当表A和表B的ID值相同时,使表A.字段a=表B.字段b,并且是批量修改。
      

  10.   

    Connected to Oracle9i Enterprise Edition Release 9.2.0.1.0 
    Connected as gdzc
    SQL> create table table1(id number(10),com1 varchar2(20));Table createdSQL> create table table2(id number(10),com2 varchar2(20));Table createdSQL> insert into table1 values(1,'01');1 row insertedSQL> insert into table1 values(2,'02');1 row insertedSQL> insert into table1 values(3,'02');1 row insertedSQL> insert into table1 values(4,'04');1 row insertedSQL> insert into table2 values(1,'aa');1 row insertedSQL> insert into table2 values(2,'bb');1 row insertedSQL> insert into table2 values(3,'bb');1 row insertedSQL> commit;Commit completeSQL> select * from table1;         ID COM1
    ----------- --------------------
              1 01
              2 02
              3 02
              4 04SQL> select * from table2;         ID COM2
    ----------- --------------------
              1 aa
              2 bb
              3 bb
    SQL> update table1 set table1.com1=(select max(table2.com2) from table2 where table1.id=table2.id) ;4 rows updated
    SQL> select * from table1;         ID COM1
    ----------- --------------------
              1 aa
              2 bb
              3 bb
              4 SQL>
      

  11.   

    update 表A set 字段a=(select 字段b from 表B where 表A.ID=表B.id)
    where exists (select 1 from 表B where 表A.ID=表B.id);
    这个语句就没问题!你执行过吗?
      

  12.   

    以上语句正确执行的条件是子查询返回数据为一行,楼主的数据中肯定存在ID重复的数据,导致子查询返回多行,引起错误。用以下语句检查:
    select id from
    (select id,count(id) cnt from 表B group by id) t
    where cnt>1
      

  13.   

    谢谢各位!!实在是不好意思是数据出了问题。
    可是我还是不清楚
    update table1 set table1.com1=(select max(table2.com2) from table2 where table1.id=table2.id) 和
    update table1 set table1.com1=(select table2.com2 from table2 where table1.id=table2.id)
    加了max() 和没加 max()为什么执行结果确实一样的?
    这事本人的最后一个问题。谢谢!!!!!!
      

  14.   

    可是我还是不清楚
    update table1 set table1.com1=(select max(table2.com2) from table2 where table1.id=table2.id) 和
    update table1 set table1.com1=(select table2.com2 from table2 where table1.id=table2.id)
    加了max() 和没加 max()为什么执行结果确实一样的?
    max(1)=1当然是成立的,呵呵
      

  15.   

    楼主你好好理解下这个sql语句的意思,注意子查询里的table1.id=table2.id,如果ID是唯一的,那么子查询select table2.com2 from table2 where table1.id=table2.id对每个id只会返回一条两个table id相等的值,既然返回一个值,那么加不加max结果当然一样