从select更新空列

fnatzsnv  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(312)

我有下表,大约300万行。

at_id  a.at_type_cd   
  1      red           
  2      blue          
  3      yellow

我想添加一列,所以我做了下面的。

alter table ship.alt
add (src_ind char(1) null)

现在我有下表:

at_id    at_type_cd   src_ind
  1      red           (null)
  2      blue          (null)
  3      yellow        (null)

src\u ind中的所有值都为空。我想根据从另一个表中选择的值向src\u ind添加值。

merge into &tableschema..alt dest
using (select /* parallel(a,16) */
    x.at_id,
    x.src_ind
    from (select
          a.at_id,
          case 
          when alt.tracks_t is null 
          then 'H' 
          when alt.tracks_at = 1 
          then 'B'
          else 'x'
          end src_ind
          from &tableschema..alt a
          left join (select 
                     alt.at_type,
                     alt.tracks_at
                     from &tableschema..at_tys@db alt
                     where alt.tracks_at <> 0) alt
                     on a.at_type_cd = alt.at_type
                     where 1=1)x ) src
                     on (src.at_id = dest.at_id) 
when matched then                                
 update set
 dest.src_ind = src.src_ind
when not matched then
insert (src_ind)
values (src.src_ind)

我这边要花两个多小时。我不确定这是否正确,使用眼球测试,有没有更有效的方法来添加基于select的列值? updateinsertmerge 也许是一个 insert 在哪里 exist ?

3pmvbmvn

3pmvbmvn1#

你的table at_tys 似乎在另一个数据库中,所以我会复制它,预先选择您需要的行:

CREATE TABLE local_tys NOLOGGING AS
SELECT at_type, at_tracks_at
  FROM &tableschema..at_tys@db alt
  WHER alt.tracks_at <> 0;

接下来,我将编写一个select来连接旧表,而不包含列和新列的值。我不完全理解你的加入,所以这只是一个模型:

SELECT a.at_id, a.at_type_cd, b.
  FROM &tableschema..alt a
  JOIN local_tys b 
    ON a.at_type_cd = b.at_type ....

然后我将从这个查询中创建一个表,一旦它构建好,就删除旧表并重命名临时表、创建索引等。

4xy9mtcn

4xy9mtcn2#

请使用下面的查询,

merge into &tableschema..alt dest
using (select 
x.at_id,
x.src_ind
from (select /* parallel(a,16) */
      a.at_id,
      case 
      when alt.tracks_t is null 
      then 'H' 
      when alt.tracks_at = 1 
      then 'B'
      else 'x'
      end src_ind
      from &tableschema..alt a
      left join (select  /* parallel(a,16) */
                 alt.at_type,
                 alt.tracks_at
                 from &tableschema..at_tys@db alt
                 where alt.tracks_at <> 0) alt
                 on a.at_type_cd = alt.at_type
                 where 1=1)x ) src
                 on (src.at_id = dest.at_id) 
when matched then                                
 update set
 dest.src_ind = src.src_ind
 when not matched then
insert (src_ind)
values (src.src_ind) ;

相关问题