简体   繁体   中英

Oracle SQL Update with Join not using table results for 'set'

I have this query that I have to convert to 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";

There's an evil join as well as what it sets are static things such as "Null" or "Resolved" and not part of another query.

I've tried various things and can't make this work. Any help?

Can you explain what is the objective of this Update? Why would you Outer join and then update on the table on the left? That leaves almost no purpose of the join itself isn't it?

If what you intended was a INNER join and update only on the resulting rows the syntax would be something like below: This would do the job for you if and only if there is a clear relationship defined by FOREIGN KEY between the 2 tables meaning there is a single record on parent (being updated) that results out of the keys from the child table.

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';

Also note your use of the field defined by name 'Date' will be a issue. That is a keyword and you are better off using a different name for the column or atleast enclose it in double quotes "Date" (put the appropriate alias also) if that is not possible.

Date would be date() (ie current date) When I try to run what you have it's saying that "ViantID" is not a valid identifyer.

(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')

That works BUT when I wrap it in the

UPDATE( QUERY HERE ) 
Set Viant_id = null  (for ease of use just one)

I get this error: QL Error: ORA-01779: cannot modify a column which maps to a non key-preserved table 01779. 00000 - "cannot modify a column which maps to a non key-preserved table" *Cause: An attempt was made to insert or update columns of a join view which map to a non-key-preserved table. *Action: Modify the underlying base tables directly.

OK! I've found that this works:

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM