Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update in Oracle

Status
Not open for further replies.

dbinfoweb

Technical User
Nov 20, 2001
59
US
Hello, I have 2 tables and I want to update the 2nd table with a column called "flag" with 1 if there is a match between the two tables.

This is what I have so far...

update table2 set FLAG = 1 where...

I joined the tables with the primary key. But it did not work.

Thanks for any help in advance.
 
update table1 a
set a.flag = ( select b.flag
from table2 b
where b.primarykey=a.primarykey)
 
Let me put my 2c:

in the vijaychin's solution there is one caveat:
if there is no record in table2 matching a.primarykey,
then the select query will return NULL as a value and this
value will populate the unmatched record of table1.

If this is what dbinfoweb wants, then it's fine,
but if not, then we have do add a where clause
to the update statement as follows:


update table1 a
set a.flag = ( select b.flag
from table2 b
where b.primarykey=a.primarykey)
where exists (select 'x'
from table2 b
where b.primarykey=a.primarykey)


Good luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top