使用select语句数据更新/合并多行

时间:2013-03-19 21:54:59

标签: oracle select merge sql-update

我尝试使用select语句的输出更新单个表,该语句返回两个值。一个是我需要在表格中更新的内容,另一个是我需要更新的信息。

Here is my select statement
select CTP.CARETEAMPATIENTID, O.ORGANIZATIONID
  from acts.careteampatient ctp,  
       ods.member m,
       ods.supplierorganization so,
       ods.MASTERSUPPLIERSUPPLIERRELATION mssr,
       ods.INSURANCEORGSUPPLIERRELATION   IOSR, 
       ods.INSURANCEORGANIZATION          IO,
       acts.organization o
where ctp.accountorgid is null
    and m.primarymemberplanid = ctp.primarymemberplanid
    and SO.AHMSUPPLIERID = M.AHMSUPPLIERID
    and mssr.SUPPLIERID       = so.SUPPLIERORGID
    AND iosr.SUPPLIERID       = so.SUPPLIERORGID
    AND io.INSURANCEORGID     = iosr.INSURANCEORGID
    and io.processingmodecd  = 'P'
    and so.usagemnemonic     = 'P'
    and O.ODSACCOUNTID = IO.INSURANCEORGID
    and O.ACCOUNTFLG = 'Y'

我想按照

的方式做点什么
update careteampatient
from (select CTP.CARETEAMPATIENTID patientid, O.ORGANIZATIONID orgid
  from acts.careteampatient ctp, 
       ods.member m,
       ods.supplierorganization so,
       ods.MASTERSUPPLIERSUPPLIERRELATION mssr,
       ods.INSURANCEORGSUPPLIERRELATION   IOSR,
       ods.INSURANCEORGANIZATION          IO,
       acts.organization o
where ctp.accountorgid is null
    and m.primarymemberplanid = ctp.primarymemberplanid
    and SO.AHMSUPPLIERID = M.AHMSUPPLIERID
    and mssr.SUPPLIERID       = so.SUPPLIERORGID
    AND iosr.SUPPLIERID       = so.SUPPLIERORGID
    AND io.INSURANCEORGID     = iosr.INSURANCEORGID
    and io.processingmodecd  = 'P'
    and so.usagemnemonic     = 'P'
    and o.odsaccountid = io.insuranceorgid
    and o.accountflg = 'Y') b  
set a.accountorgid = b.orgid
where a.careteampatientid = b.patientid

这是我尝试过的合并

merge into careteampatient a
using (select CTP.CARETEAMPATIENTID patientid, O.ORGANIZATIONID orgid
        from acts.careteampatient ctp, 
             ods.member m,
             ods.supplierorganization so,
             ods.MASTERSUPPLIERSUPPLIERRELATION mssr,
             ods.INSURANCEORGSUPPLIERRELATION   IOSR,
             ods.INSURANCEORGANIZATION          IO,
             acts.organization o
      where ctp.accountorgid is null
          and m.primarymemberplanid = ctp.primarymemberplanid
          and SO.AHMSUPPLIERID = M.AHMSUPPLIERID
          and mssr.SUPPLIERID       = so.SUPPLIERORGID
          AND iosr.SUPPLIERID       = so.SUPPLIERORGID
          AND io.INSURANCEORGID     = iosr.INSURANCEORGID
          and io.processingmodecd  = 'P'
          and so.usagemnemonic     = 'P'
          and o.odsaccountid = io.insuranceorgid
          and o.accountflg = 'Y') b
on a.careteampatientid = b.patientid
when matched then
  update 
    set a.accountorgid = b.orgid

这不起作用,我是新鲜的想法,任何帮助都会很棒。感谢

1 个答案:

答案 0 :(得分:0)

想出来,我的ON上缺少括号。它应该是on (a.careteampatientid = b.patientid)我的选择也应该是select distinct