?
Solved

Oracle Update Query Help

Posted on 2011-09-08
8
Medium Priority
?
285 Views
Last Modified: 2012-05-12
Hello,

I am mostly a SQL Server user, and I have need to create an update query in Oracle.  Below is my query from SQL Server, how would that translate to Oracle SQL?

Thanks,
-Torrwin
UPDATE Master_ID_Table
SET Table2_ID = B.ID2
FROM Master_ID_Table A INNER JOIN (SELECT Table1.ID AS ID1, Table2.ID AS ID2 FROM Table1 INNER JOIN Table2 ON Table1.Field = Table2.Field) B ON A.Table1_ID = B.ID1
WHERE A.Table2_ID IS NULL;

Open in new window

0
Comment
Question by:Torrwin
[X]
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
  • 4
  • 3
8 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 36504929
how about this?
ee.txt
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 total points
ID: 36504951
or this...
ee.txt
0
 
LVL 13

Author Comment

by:Torrwin
ID: 36505281
Thanks, those both make sense.  I kicked off the second one and it's been going for some time now.  From an efficiency standpoint, is the first one better since it's part of the join and not the where clause?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 74

Expert Comment

by:sdstuber
ID: 36505560
shouldn't really matter efficiencywise because they are inner joins so it should be a pushable predicate.
0
 
LVL 13

Author Comment

by:Torrwin
ID: 36506135
Hmm, neither of these seem to be working.  The first give me a "ORA-00904:'A'.'table1_id': invalid identifier" error on the extra join, and the second gives me a "ORA-01427: single-row subquery returns more than one row" error.  I have re-verified that all the source tables contain unique values.

Thoughts?
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36509997
My ansi join syntax is pretty weak and I likely have the syntax wrong but try an update like this:

UPDATE (
select b.id2, table2_id
FROM Master_ID_Table A INNER JOIN (SELECT Table1.ID AS ID1, Table2.ID AS ID2 FROM Table1 INNER JOIN Table2 ON Table1.Field = Table2.Field) B ON A.Table1_ID = B.ID1
WHERE A.Table2_ID IS NULL)
SET Table2_ID = ID2;
0
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 2000 total points
ID: 36510012
if your're getting ORA-01427 then you're not going to be able to update your data.


select * from (
select a.table1_id, (SELECT count(*)
              FROM (SELECT table1.id AS id1, table2.id AS id2
                      FROM table1 INNER JOIN table2 ON table1.field = table2.field) b
             WHERE a.table1_id = b.id1) cnt
from master_id_table a
) where cnt > 1

after looking at those id's and the corresponding id2's check if it's the same value being returned more than once, if so, just add distinct


UPDATE master_id_table a
   SET a.table2_id      =
           (SELECT DISTINCT id2
              FROM (SELECT table1.id AS id1, table2.id AS id2
                      FROM table1 INNER JOIN table2 ON table1.field = table2.field) b
             WHERE a.table1_id = b.id1)
 WHERE a.table2_id IS NULL;
0
 
LVL 13

Author Closing Comment

by:Torrwin
ID: 36524456
Thanks!  Apparently even though my separate tables consisted of distinct values, when combined together they had a couple of duplicate ID's.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

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…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
Suggested Courses

770 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