1

I have an update query like following:

update table TABLE1 set COL1 = 'X' where COL2 = 'Y' ---1

Support the values 'X' and 'Y' are fetched from database now TABLE2. E.g.

select COL1, COL2 from TABLE2. ----2

I want to update table TABLE1 with values from TABLE2.

Just to make it more clear, assume that TABLE2 has following values:

alt text

Can you please help me in doing this in a single query!

I am using Oracle 11g.

5
  • 1
    @Sandeep Jindal, What is your RDBMS? Commented Jan 14, 2011 at 11:45
  • decipherinfosys.wordpress.com/2007/01/31/… Commented Jan 14, 2011 at 11:51
  • 1
    Why don't you say what is your RDBMS?? Commented Jan 14, 2011 at 11:56
  • @marc_s: Thanks for suggest. Work on the same! Improvement shall be reflected soon (24 hours) :) Commented Jan 14, 2011 at 12:28
  • @Sandeep You might be interested in this stack-exchange proposal. It's almost ready to begin beta, just needs a few more. Commented Jan 19, 2011 at 4:48

4 Answers 4

2

For Oracle, this is the most basic way to do it:

update TABLE1
  set COL1 = (select TABLE2.COL1 from TABLE2 where TABLE2.COL2 = TABLE1.COL2)
  where COL2 IN (select TABLE2.COL2 from TABLE2);

This can be inefficient in some cases since it could execute a subquery for every row in TABLE1.

Depending on the declaration of primary key or unique constraints on both tables, you may be able to use the updateable inline-view method, which is probably more efficient:

update
  (select TABLE1.COL1 as T1C1, TABLE1.COL2 as T1C2, TABLE2.COL1 as T2C1
     from TABLE1 join TABLE2 on TABLE2.COL2 = TABLE1.COL2
  )
  set T1C1 = T2C1;
Sign up to request clarification or add additional context in comments.

Comments

2

@Dave Costa's answer is correct, if you limit yourself to update statements. However, I've found that using a merge statement in these situations allows me to do this in a more straightforward manner:

merge into TABLE1 
      using TABLE2 
      on (TABLE2.COL2 = TABLE1.COL2)
when matched then
     update set TABLE1.COL1 = TABLE2.COL1;

Comments

1
update TABLE1 
set TABLE1.COL1 = TABLE2.COL1
from TABLE1
join TABLE2 on TABLE1.COL2 = TABLE2.COL2

(this would work on Sql Server)

1 Comment

Don't you think that table keyword is redundant here update table TABLE1 ?
-3

for oracle:

UPDATE Table1 t1
 SET (X,Y) = (SELECT X,Y from Table2 WHERE ...YourConditions...)
WHERE ... Another Conditions ...

for mysql, sql-server

UPDATE t1
 SET t1.X = t2, t2.Y = t2.Y
FROM Table1 t1, Table2 t2
WHERE t1.Something = t2.Something

2 Comments

MySQL's UPDATE statement doesn't have a FROM clause.
I need to update TABLE1, as following: update table TABLE1 set COL1 = 'X' where COL2 = 'Y' . Both the values(lists) is gathered from TABLE2. I am not sure if your query: UPDATE Table1 t1 SET (X,Y) = (SELECT X,Y from Table2 WHERE ...YourConditions...) Suppose I don’t have 'Another Conditions'. Would not this query update all the value of TABLE2 (and not just values of COL1 for which COL2 matches in TABLE2)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.