Solved

Conditional Oracle SQL Update statement

Posted on 2015-01-09
6
466 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

763 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