Update table from another table [message #495916] |
Wed, 23 February 2011 04:40  |
koff10
Messages: 58 Registered: December 2006 Location: france
|
Member |
|
|
Hi ALL,
I want to update table1 from table2
I wrote statement like this:
UPDATE
TABLE1 t
JOIN TABLE2 tmp
ON t.username = tmp.username
SET
t.password = tmp.password;
It works fine in my MYSQL DATABASE.
BUT in ORACLE 9.2 DATABASE, I got Error like this:
JOIN TABLE2 tmp
*
ERROR at line 3:
ORA-00971: missing SET keyword
I tried inner join or natural join => same error.
May be I missed somethings ?
Thanks for help.
koff10
|
|
|
|
Re: Update table from another table [message #495920 is a reply to message #495916] |
Wed, 23 February 2011 04:46   |
cookiemonster
Messages: 13966 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Either:
UPDATE TABLE1 t
SET PASSWORD = (SELECT PASSWORD
FROM TABLE2
WHERE username = t.username)
WHERE EXISTS (SELECT NULL FROM
FROM TABLE2
WHERE username = t.username);
or, assuming username has a pk or unique index on it in both tables:
UPDATE (SELECT t.password old_pass, tmp.password new_pass
FROM TABLE1 t
JOIN TABLE2 tmp
ON t.username = tmp.username)
SET old_pass = new_pass;
|
|
|
|
Re: Update table from another table [message #496093 is a reply to message #495920] |
Thu, 24 February 2011 07:21   |
koff10
Messages: 58 Registered: December 2006 Location: france
|
Member |
|
|
[quote title=cookiemonster wrote on Wed, 23 February 2011 11:46]Either:
UPDATE TABLE1 t
SET PASSWORD = (SELECT PASSWORD
FROM TABLE2
WHERE username = t.username)
WHERE EXISTS (SELECT NULL FROM
FROM TABLE2
WHERE username = t.username);
Thanks All for your helps.
But for this Scripts there is sitill error:
ORA-01427: single-row subquery returns more than one row
|
|
|
|
Re: Update table from another table [message #496095 is a reply to message #496093] |
Thu, 24 February 2011 07:29  |
flyboy
Messages: 1903 Registered: November 2006
|
Senior Member |
|
|
This error indicates problem in your data. There are multiple rows with the same USERNAME column value in TABLE2 table. However, column TABLE1.PASSWORD may be updated only with one value.
The question on you is: which of them shall be taken?
After you determine it, enhance the UPDATE statement with additional condition covering it.
|
|
|