Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Nested Subselect in UPDATE statement

GwydionMar 9 2010 — edited Mar 10 2010
Hi everybody,

I have the following problem: There are two tables FOO and BAR that both have columns X and Y and a primary key. I want to fill the values X and Y of FOO with the values X and Y from BAR where FOO.key = BAR.key . There may be multiple rows from BAR with the same key so I simply select the first one. The query would look like
UPDATE foo f
   SET ( x, y ) = ( SELECT *
                      FROM ( SELECT b.x, b.y
                               FROM bar b
                              WHERE f.key = b.key )                     
                     WHERE ROWNUM = 1 );
However, this doesn't work because the alias F is not known inside the nested subquery. If I remove the outer subquery ( the one with ROWNUM=1 ) it works fine. But I need to limit the result to a single row ( in the real application I sort the innermost query by date ). Why does Oracle forget the meaning of alias F within the nested subquery?

Pat
This post has been answered by Lakmal Rajapakse on Mar 9 2010
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 7 2010
Added on Mar 9 2010
6 comments
32,690 views