从两个表更新oracle

时间:2018-07-11 07:06:27

标签: oracle join sql-update

我有两个桌子。

  1. J_YAD_PICT(表名称),GALLERY_DISP_FLG(列)
  2. J_YAD_CGR_PICT(表名称),Y_CGR_FLG(列)

当J_YAD_CGR_PICT中的Y_CGR_FLG不为null时,则需要将GALLERY_DISP_FLG设置为1。

我已经执行了

Update A
SET A.GALLERY_DISP_FLG = 
  (CASE WHEN b.Y_CGR_FLG IS NOT NULL) THEN '1' 
        ELSE A.GALLERY_DISP_FLG 
   END
FROM J_YAD_PICT A inner join J_YAD_CGR_PICT B 
on A.YP_ID = B.YP_ID;

但是鉴于此错误:

  

SQL错误:ORA-00905:缺少关键字

有人可以帮助我吗?

3 个答案:

答案 0 :(得分:0)

您使用哪个表别名A?

我建议这样尝试

Update J_YAD_PICT A SET 
  A.GALLERY_DISP_FLG =   (CASE WHEN b.Y_CGR_FLG IS NOT NULL) THEN '1'  
                               ELSE A.GALLERY_DISP_FLG 
                         END 
FROM J_YAD_PICT A inner join J_YAD_CGR_PICT B  on A.YP_ID = B.YP_ID;

答案 1 :(得分:0)

像这样吗?

while (<>) {  # reads line into $_
    next if 1..4;
    chomp;    # automatically uses $_ variable
    match_replace($_);
} continue { close ARGV if eof }  # needed for $. (and range operator)

update j_yad_pict p set p.gallery_disp_flg = (select case when c.y_cgr_flg is not null then '1' else p.gallery_disp_flg end from j_yad_cgr_pict c where c.yp_id = p.yp_id ) where exists (select null from j_yad_cgr_pict c1 where c1.yp_id = p.yp_id ); 的查询部分位于此处,因此您不会将EXISTS表中不匹配的行(更新为NULL)。

答案 2 :(得分:0)

更新表J_YAD_PICT时,必须将此表与内部选择中的表连接

UPDATE J_YAD_PICT A
SET A.GALLERY_DISP_FLG = (
    SELECT CASE WHEN b.Y_CGR_FLG IS NOT NULL THEN '1' ELSE A.GALLERY_DISP_FLG END 
    FROM J_YAD_PICT A2 
        INNER JOIN J_YAD_CGR_PICT B ON A.YP_ID = B.YP_ID 
        WHERE A.ID = A2.ID);