Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Conditional Oracle SQL Update statement

Posted on 2015-01-09
6
465 Views
Last Modified: 2015-01-13
Hi, I'm back once again to display my cluelessness to the world :)

I was wondering if anyone could tell me the best way to compose an SQL UPDATE statement.
I'll describe the situation I've got.

We have two tables relevant to the problem:

Table name ORDER_CONTACT
Fields: INVOICE_NO (text), ORDER_CONTACT_ID (number)

Table name AUTO_EFORM
Fields: CONTACT_ID (number), SUBJECT (text)

the text in the SUBJECT field of each record in AUTO_EFORM contains an invoice number. We will be isolating that invoice number using code as you'll see below.


What I'm trying to achieve is this (forgive the pseudocode)

If <invoice number within AUTO_EFORM.SUBJECT> = ORDER_CONTACT.INVOICE_NO
and ORDER_CONTACT.SO_CONTACT_ID is not blank
then replace AUTO_EFORM.CONTACT_ID 
with ORDER_CONTACT.SO_CONTACT_ID

Open in new window


Now, here is my SQL code.

update auto_eform
set contact_id = 
select 
order_contact_id 
from 
order_contact, auto_eform ae
where substr(ae.subject, 10, (instr(ae.subject, ' ', 10)-10)) = order_contact.invoice_no

Open in new window


The code almost works properly, but it does not cater for cases where order_contact.contact_id is null.
Can anyone tell me how to adapt it so that the UPDATE only takes place if order_contact.contact_id has a valid value please?

Thanks for looking!
0
Comment
Question by:vistamed
6 Comments
 
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 200 total points
ID: 40540592
When dealing with two tables and an update statement, I immediately think of MERGE:
http://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_9016.htm#SQLRF01606

You can add the logic you want to the ON clause.
0
 

Author Comment

by:vistamed
ID: 40540608
Hi Slightvw,
Oooooo I am liking the look of that! I'll know better on Monday cos it's HOMETIME YAAAY and I'm running out the door, but I'd say you've got a winner there :) Thank you for your help!
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 40540622
www.experts-exchange.com/Database/Miscellaneous/A_1517-UPDATES-with-JOIN-for-everybody.html

Looks like you're really close.  What if you add another condition to the WHERE clause?

update auto_eform
set contact_id = 
select 
order_contact_id 
from 
order_contact, auto_eform ae
where substr(ae.subject, 10, (instr(ae.subject, ' ', 10)-10)) = order_contact.invoice_no
and  order_contact_id is not null

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 32

Accepted Solution

by:
awking00 earned 300 total points
ID: 40540963
I also like the merge concept. You might try like the following:
merge into auto_eform ae
using
(select contact_id, invoice_no
 from order_contact
 where contact_id is not null) oc
on oc.invoice_no = substr(ae.subject,10, instr(ae.subject,' ',10) -10))
when matched then
update set ae.contact_id = oc.contact_id;

If this is satisfactory to you, please give slightwv credit and not me.
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40540975
>>please give slightwv credit and not me

I'm fine with a split since you did the actual legwork!!!  ;)
0
 

Author Closing Comment

by:vistamed
ID: 40547147
Hi, awking00's MERGE statement was pretty much perfect - the only tweak it needed to run was to add parentheses around the conditions after the ON keyword. Once they were added, I was on the pig's back. Thanks to you both, slightwv and awking00!
Daniel, I had been struggling with the same approach you were suggesting for quite a while but I just couldn't make it work I'm afraid. The SQL grammar fairy just didn't seem to like the fact that I was referring to a table (order_contact) in the UPDATE statement when that table had originated in the SELECT statement, if you know what I mean. Thank you for your help though!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to Gracefuly recover in Racle stored procedure 1 34
Problem with duplicate records in Oracle query 16 40
sum of columns in a row in oracle 3 33
oracle forms question 22 42
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question