更新集从选择错误语法

时间:2016-08-03 12:28:38

标签: sql oracle select syntax nested

我尝试使用上面的更新设置,我尝试了不同类型的语法,但没有成功

update 
pc_discount
set 
ceza_ucret_turu_id=pc_discount_ceza.CEZA_ALT_UCRET_KODU 
from 
(select CEZA_ALT_UCRET_KODU from pc_discount_ceza where CEZA_ALT_UCRET_KODU is not null) pdc
where 
pc_discount.id=pdc.discount_id
ceza_ucret_turu_id is null

第一个没有成功(sql命令没有正确完成)

MERGE INTO pc_discount pd
  USING pc_discount_ceza pdc
  ON pd.id = pdc.discount_id
     AND pdc.CEZA_ALT_UCRET_KODU is not null
     and pd.ceza_ucret_turu_id is null
WHEN MATCHED THEN
 UPDATE 
  SET ceza_ucret_turu_id = pdc.CEZA_ALT_UCRET_KODU

再次没有成功(缺少ON关键字)

有没有建议运行那种sql?

编辑:

update 
pc_discount
set 
ceza_ucret_turu_id=pdc.CEZA_ALT_UCRET_KODU 
from 
(select CEZA_ALT_UCRET_KODU from pc_discount_ceza where CEZA_ALT_UCRET_KODU is not null) pdc
where 
pc_discount.id=pdc.discount_id and
ceza_ucret_turu_id is null

仍然没有变化。

2 个答案:

答案 0 :(得分:1)

我会改变一些事情

  • 有两个条件,他们需要加入“AND”设置
  • 使用集合
  • 中的表别名
  • 将加入条件添加到子查询

尝试类似

的内容
update 
  pc_discount
set 
  ceza_ucret_turu_id = pcd.CEZA_ALT_UCRET_KODU 
from 
  ( 
     select 
       discount_id, 
       CEZA_ALT_UCRET_KODU 
     from 
       pc_discount_ceza 
     where
       CEZA_ALT_UCRET_KODU is not null
  ) pcd 
where 
  pc_discount.id = pcd.discount_id and 
  pc_discount.ceza_ucret_turu_id is null

答案 1 :(得分:-1)

试试这个 - 我不确定你是否正在使用Oracle的正确语法

UPDATE pc_discount    pd
SET ceza_ucret_turu_id =
 (SELECT
   MAX(ceza_alt_ucret_kodu)
  FROM 
   pc_discount_ceza  pdc
  WHERE pd.id = pdc.discount_id
 )
WHERE pd.ceza_ucret_turu_id IS NULL
;

以上内容已根据您的新业务规则进行了更新