Home » SQL & PL/SQL » SQL & PL/SQL » Update statement where update exists. (Oracle 12.1.0.1.0 Window)
Update statement where update exists. [message #613259] Thu, 01 May 2014 15:03 Go to next message
tmcallister
Messages: 107
Registered: December 2007
Senior Member
I have a query of the following form:

UPDATE   address ad
SET      (area_id, street_id)      =
           <COMPLICATED QUERY>
WHERE    EXISTS
           <result from COMPLICATED QUERY>


And I can't help but think there has to be a way to write this without duplicating the <COMPLICATED QUERY>. Any tips?
Re: Update statement where update exists. [message #613260 is a reply to message #613259] Thu, 01 May 2014 15:32 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Why do you want to avoid the EXISTS clause?
It is there to make sure that only those records which are picked in the subquery are updated, else all the records would be updated. And just imagine what would the update do to those records which are not in the subquery? I don't see any harm with the EXISTS clause.
Re: Update statement where update exists. [message #613261 is a reply to message #613260] Thu, 01 May 2014 15:45 Go to previous messageGo to next message
tmcallister
Messages: 107
Registered: December 2007
Senior Member
Oh I fully realize what purpose it serves, I just hate seeing a block of SQL duplicated twice (especially when it's ugly/complex). Whenever I see that I always think that there must be a way I'm not aware of to represent it better.

[Updated on: Thu, 01 May 2014 15:45]

Report message to a moderator

Re: Update statement where update exists. [message #613262 is a reply to message #613260] Thu, 01 May 2014 15:45 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
><result from COMPLICATED QUERY>
must return only single row
Re: Update statement where update exists. [message #613263 is a reply to message #613262] Thu, 01 May 2014 15:48 Go to previous messageGo to next message
tmcallister
Messages: 107
Registered: December 2007
Senior Member
Like so?

UPDATE   address ad
SET      (area_id, street_id)      =
          (SELECT new_area_id, new_street_id
           FROM <COMPLICATED JOINS>
           WHERE ad.ID = local.ID)
WHERE    EXISTS
          (SELECT 1
           FROM <COMPLICATED JOINS>
           WHERE ad.ID = local.ID)
Re: Update statement where update exists. [message #613269 is a reply to message #613263] Fri, 02 May 2014 00:25 Go to previous messageGo to next message
Michel Cadot
Messages: 68757
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Try MERGE:
merge into address a
      using <COMPLICATED JOINS> local
      on ad.ID = local.ID
when matched then update set area_id=new_area_id, street_id=new_street_id
when not matched then insert (id) values (local.id) where 1=2
/

Re: Update statement where update exists. [message #641760 is a reply to message #613263] Fri, 21 August 2015 10:37 Go to previous message
Michel Cadot
Messages: 68757
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Any feedback?

Previous Topic: Returning a sys_refcursor from an explicit cursor
Next Topic: Oracle Hierarchical Query max_level/connect_by_is_root
Goto Forum:
  


Current Time: Sun Apr 27 07:16:15 CDT 2025