通过联接4个表来更新sql表

时间:2018-07-24 14:16:27

标签: sql oracle join sql-update ora-00933

我试图通过连接4个表来更新sql表,下面是查询

UPDATE DC 
SET M_CA_PRO_ELG  = CPTY_UDF.M_CA_PRO_ELG
FROM COMD_TABLE DC
JOIN EXT_TABLE TRN_EXT ON DC.M_NB = TRN_EXT.M_REF
JOIN HDR_TABLE TRN_HDR ON TRN_EXT.M_TR_REF = TRN_HDR.M_NB 
JOIN CPDF_TABLE CPDF ON TRN_HDR.M_COUNTRPART= CPDF.M_ID
JOIN COUNTERP_TABLE CPTY_UDF ON CPDF.M_LBL=CPTY_UDF.M_LBL 
WHERE TRN_HDR.M_STATUS <> 'FISHING'

下面是遇到的错误

  

错误报告:   SQL错误:ORA-00933:SQL命令未正确结束   00933. 00000-“ SQL命令未正确结束”

更新1: 01407说,此版本也不起作用。00000-“无法将(%s)更新为NULL”,但是选择中根本没有空值

 update COMD_TABLE DC SET DC.M_CA_PRO_ELG = 
(
  select CPTY_UDF.M_CA_PRO_ELG  from COUNTERP_TABL CPTY_UDF,EXT_TABLE TRN_EXT,HDR_TABLE TRN_HDR ,CPDF_TABLE CPDF
  where DC.M_NB = TRN_EXT.M_REF and  TRN_EXT.M_TR_REF = TRN_HDR.M_NB  and TRN_HDR.M_COUNTRPART= CPDF.M_ID and CPDF.M_LBL=CPTY_UDF.M_LBL 
  and  TRN_HDR.M_STATUS <> 'FISHING'
)

更新2:

WITH CA_PROVINCE_DATA AS 
(
 SELECT CPTY_UDF.M_CA_PRO_ELG AS CA_PRO ,DC.M_NB AS M_NB   
 FROM EXT_TABLE 
 JOIN HDR_TABLE TRN_HDR ON TRN_EXT.M_TR_REF = TRN_HDR.M_NB 
 JOIN CPDF_TABLE CPDF ON TRN_HDR.M_COUNTRPART= CPDF.M_ID
 JOIN COUNTERP_TABLE CPTY_UDF ON CPDF.M_LBL=CPTY_UDF.M_LBL 
 WHERE TRN_HDR.M_STATUS <> 'FISHING'
)
 UPDATE COMD_TABLE SET M_CA_PRO_ELG = CA_PROVINCE_DATA.CA_PRO WHERE 
 M_NB= CA_PROVINCE_DATA.M_NB

此查询在ORACLE中有效还是有其他方法可以实现?

感谢您的帮助

谢谢

2 个答案:

答案 0 :(得分:0)

1。

update ( select sal, new_sal
     from SCOTT.EMP o
     join ( select empno, sal/10 as new_sal
            from scott.emp ) n
      on (o.empno = n.empno) )
set sal = new_sal;

2。

merge into SCOTT.EMP o
using ( select empno, sal/10 as new_sal
     from scott.emp ) i 
 on ( o.empno = i.empno )
when matched then update
 set o.sal = i.new_sal;

答案 1 :(得分:0)

对我来说,这似乎是一个SQL Server更新。我喜欢的SQL Server功能。我相信下面是您正在寻找的Oracle更新。如果要更新所有记录,请忽略“存在的位置”部分。

UPDATE dc
   SET m_ca_pro_elg   =
           (SELECT cpty_udf.m_ca_pro_elg
              FROM ext_table  trn_ext
                   JOIN hdr_table trn_hdr ON trn_ext.m_tr_ref = trn_hdr.m_nb
                   JOIN cpdf_table cpdf ON trn_hdr.m_countrpart = cpdf.m_id
                   JOIN counterp_table cpty_udf
                       ON cpdf.m_lbl = cpty_udf.m_lbl
             WHERE dc.m_nb = trn_ext.m_ref AND trn_hdr.m_status <> 'FISHING')
 WHERE EXISTS
           (SELECT NULL
              FROM ext_table  trn_ext
                   JOIN hdr_table trn_hdr ON trn_ext.m_tr_ref = trn_hdr.m_nb
                   JOIN cpdf_table cpdf ON trn_hdr.m_countrpart = cpdf.m_id
                   JOIN counterp_table cpty_udf
                       ON cpdf.m_lbl = cpty_udf.m_lbl
             WHERE dc.m_nb = trn_ext.m_ref AND trn_hdr.m_status <> 'FISHING')