Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Conditional Oracle SQL Update statement

Posted on 2015-01-09
6
Medium Priority
?
473 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 78

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 800 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 32

Accepted Solution

by:
awking00 earned 1200 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 78

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
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…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Suggested Courses

886 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