如何使用条件子句更新现有记录?

时间:2011-12-07 11:23:18

标签: sql oracle

我是Oracle SQL新手所以我有一个问题..我有两个表,表A和表B.现在表A和表B具有相同的列名,但在表A中,只有一列(名为'tracker')实际上有数据。表A中的其余列是空的...我需要做的是更新表A中的每个记录,以便从表B复制其他列的值,条件是表A中的'tracker'列值与表B中的'tracker'列匹配。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

MERGE INTO tableA a
USING tableB b
ON (a.tracker=b.tracker)
WHEN MATCHED THEN UPDATE SET  
  a.column1=b.column1,
  a.column2=b.column2;

如果B中存在A中不存在的行:

MERGE INTO tableA a
USING tableB b
ON (a.tracker=b.tracker)
WHEN MATCHED THEN UPDATE SET  
  a.column1=b.column1,
  a.column2=b.column2
WHEN NOT MATCHED THEN INSERT VALUES
  a.tracker,a.column1,a.column2; --all columns

答案 1 :(得分:2)

create  table a (somedata varchar2(50), tracker number , constraint pk_a primary key (tracker));
create  table b (somedata varchar2(50), tracker number, constraint pk_b primary key (tracker));
/

--insert some data
insert into a (somedata, tracker)
select 'data-a-' || level, level
  from dual
  connect by level < 10;

insert into b (somedata, tracker)  
  select 'data-b-' || -level, level
  from dual
  connect by level < 10;



  select * from a;
SOMEDATA                                           TRACKER
-------------------------------------------------- -------
data-a-1                                                 1 
data-a-2                                                 2 
data-a-3                                                 3 
data-a-4                                                 4 
data-a-5                                                 5 
data-a-6                                                 6 
data-a-7                                                 7 
data-a-8                                                 8 
data-a-9                                                 9 

  select * from b;
  SOMEDATA                                           TRACKER
-------------------------------------------------- -------
data-b--1                                                1 
data-b--2                                                2 
data-b--3                                                3 
data-b--4                                                4 
data-b--5                                                5 
data-b--6                                                6 
data-b--7                                                7 
data-b--8                                                8 
data-b--9                                                9 

  commit;


   update (select a.somedata a_somedata, b.somedata b_somedata
             from a 
                  inner join
                  b
                    on a.tracker = b.tracker)
    set 
          a_somedata = b_somedata;


select * from a;          --see below for results--

         --or you can do it this way: (issuing rollback to get data back in previous state)
         --for a one column update, either way will work, I would prefer the former in case there is a multi-column update necessary
         -- merge *as posted by another person* will also work

         update a
           set somedata = (select somedata 
                             from b
                            where a.tracker = b.tracker
                            );

          select * from A; --see below for results--

          -- clean up
          -- drop table a;
          -- drop table b; 

这会给你结果:

SOMEDATA                                           TRACKER
-------------------------------------------------- -------
data-b--1                                                1 
data-b--2                                                2 
data-b--3                                                3 
data-b--4                                                4 
data-b--5                                                5 
data-b--6                                                6 
data-b--7                                                7 
data-b--8                                                8 
data-b--9                                                9 

这是oracle关于UPDATE

的文档的链接