具有两个或多个内连接oracle的update语句和计算值也从视图中选择

时间:2016-12-20 14:58:27

标签: sql oracle join sql-update inner-join

我收到一条错误,指出未找到FROM Keyword。请让我知道下面的代码可能有什么问题。我正在尝试使用两个内部联接进行更新,并从另一个视图执行选择。请告诉我如何在oracle中完成此任务。

  update (Select asset.CRV_AMOUNT as ACRV, 
                 cmd.CRV_PERCENT  as CRVP, 
                 (select CRV$ 
                    from recapt.facility_rec_crv_V fac_v 
                   where fac_v.fac_code = fac.code 
                     AND fac_v.complex_code = fac.complex_code) CRV_TOTAL as CRVT 
            from recapt.asset asset
                 inner join  recapt.facility_rec fac
                    on fac.code = asset.fac_rec_code and 
                       fac.complex_code = asset.complex_code
                 inner join recapt.cost_model_detail cmd
                    on cmd.cost_model_id = fac.cost_model_id and
                       cmd.mf_division_id = asset.mf_division_id) t
     SET t.ACRV = ((t.CRVP * t.CRVT)/100);

1 个答案:

答案 0 :(得分:2)

您可以尝试切换到MERGE语句 - 编码和理解可能会更容易。 我不能测试这个,但有些内容符合:

merge into recapt.asset asset
using (select fac.code,
              fac.complex_code,
              cmd.mf_division_id,
              cmd.crv_percent as crvp,
              (select crv$
                 from recapt.facility_rec_crv_v fac_v
                where fac_v.fac_code = fac.code
                  and fac_v.complex_code = fac.complex_code) as crvt
         from recapt.facility_rec fac
        inner join recapt.cost_model_detail cmd
           on cmd.cost_model_id = fac.cost_model_id) t
    on (asset.fac_rec_code = t.code and
        asset.complex_code = t.complex_code and
        asset.mf_division_id = t.mf_division_it)
  when matched then
    update set asset.acrv = (t.crvp * t.crvt) / 100;

如果您提供create table和insert语句(包含少量测试数据),我可以测试这个和/或提供更好的解决方案。