带有Join的Oracle SQL Update不使用'set'的表结果

时间:2013-10-23 18:06:48

标签: sql oracle join

我有这个查询,我必须转换为Oracle SQL。

UPDATE Active_Exclusions
LEFT JOIN Active_Exclusions_Full ON 

Active_Exclusions.Exclude_Reason = Active_Exclusions_Full.Exclude_Reason AND 
Active_Exclusions.ViantID = Active_Exclusions_Full.ViantID 

SET Active_Exclusions.ViantID = Null, 
Active_Exclusions.Date_Resolved = Date,
Active_Exclusions.Resolution = "Resolved"

WHERE Active_Exclusions.ViantID Is Not Null AND 
Active_Exclusions_Full.ViantID Is Null AND 
Active_Exclusions.Exclude_Reason<>"PCP not in RSA County";

有一个邪恶的连接以及它设置的是静态的东西,如“Null”或“Resolved”,而不是另一个查询的一部分。

我尝试了各种各样的东西,但却无法做到这一点。有什么帮助吗?

3 个答案:

答案 0 :(得分:0)

您能解释一下此更新的目标是什么吗?为什么你会在左边的桌子上加入然后更新?这几乎没有任何连接本身的目的不是吗?

如果你想要的是INNER连接并且只对结果行进行更新,则语法如下所示:当且仅当2个表之间存在由FOREIGN KEY定义的明确关系时,这将为您完成工作意味着在父表(正在更新)上有一条记录来自子表中的键。

UPDATE (SELECT ae.ViantID, ae.Date_Resolved, ae.Resolution
          FROM Active_Exclusions ae, Active_Exclusions_Full aef
         WHERE ae.Exclude_Reason =
                  aef.Exclude_Reason
               AND ae.ViantID = Active_Exclusions_Full.ViantID
               AND ae.ViantID IS NOT NULL
               AND aef.ViantID IS NULL
               AND ae.Exclude_Reason <>
                      'PCP not in RSA County')
   SET ViantID = NULL, Date_Resolved = Date, Resolution = 'Resolved';

另请注意,您使用名称“日期”定义的字段将是一个问题。这是一个关键字,你最好为列使用不同的名称,或者至少将它用双引号“Date”括起来(如果不可能的话也加上相应的别名)。

答案 1 :(得分:0)

日期将是日期()(即当前日期)当我尝试运行您所拥有的内容时,它说“ViantID”不是有效的标识符。

(SELECT agp_mpi_prov_quality_history.viant_id, AGP_MPI_PROV_QUALITY_HISTORY.RESOLUTION1
FROM AGP.AGP_MPI_PROV_QUALITY_HISTORY AGP_MPI_PROV_QUALITY_HISTORY, 
AGP.AGP_MPI_PROV_QUALITY_CURRENT AGP_MPI_PROV_QUALITY_CURRENT
    WHERE AGP_MPI_PROV_QUALITY_HISTORY.Exclude_Reason = AGP_MPI_PROV_QUALITY_current.Exclude_Reason
    AND AGP_MPI_PROV_QUALITY_HISTORY.Viant_ID = AGP_MPI_PROV_QUALITY_current.Viant_ID
    AND AGP_MPI_PROV_QUALITY_HISTORY.Viant_ID IS NOT NULL
    AND AGP_MPI_PROV_QUALITY_current.Viant_ID IS NULL
    AND AGP_MPI_PROV_QUALITY_HISTORY.Exclude_Reason <> 'PCP not in RSA County')

当我将它包装在

中时,它可以工作
UPDATE( QUERY HERE ) 
Set Viant_id = null  (for ease of use just one)

我收到此错误:QL错误:ORA-01779:无法修改映射到非密钥保留表的列 01779. 00000 - “无法修改映射到非密钥保留表的列” *原因:尝试插入或更新连接视图的列            映射到非密钥保留的表。 *操作:直接修改基础表。

答案 2 :(得分:0)

OK!我发现这有效:

update agp_mpi_prov_quality_history
set viant_id = null
where exists
   (SELECT agp_mpi_prov_quality_history.viant_id
   FROM AGP.AGP_MPI_PROV_QUALITY_HISTORY AGP_MPI_PROV_QUALITY_HISTORY, 
   AGP.AGP_MPI_PROV_QUALITY_CURRENT AGP_MPI_PROV_QUALITY_CURRENT
   WHERE AGP_MPI_PROV_QUALITY_HISTORY.Exclude_Reason = AGP_MPI_PROV_QUALITY_current.Exclude_Reason
   AND AGP_MPI_PROV_QUALITY_HISTORY.Viant_ID = AGP_MPI_PROV_QUALITY_current.Viant_ID
   AND AGP_MPI_PROV_QUALITY_HISTORY.Viant_ID IS NOT NULL
   AND AGP_MPI_PROV_QUALITY_current.Viant_ID IS NULL
   AND AGP_MPI_PROV_QUALITY_HISTORY.Exclude_Reason <> 'PCP not in RSA County')