Unix Technical Forum

Help with SQL - Update between tables

This is a discussion on Help with SQL - Update between tables within the Oracle Miscellaneous forums, part of the Oracle Database category; --> Here is what I am trying to achieve in sudo code: update table1 set table1.fname = table2.fname, table1.lname = ...


Go Back   Unix Technical Forum > Database Server Software > Oracle Database > Oracle Miscellaneous

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-08-2008, 01:06 PM
Problematic coder
 
Posts: n/a
Default Help with SQL - Update between tables

Here is what I am trying to achieve in sudo code:

update table1 set
table1.fname = table2.fname,
table1.lname = table2.lname
where table1.emplid = table2.emplid;

Now clearly this is wrong since it gives me an error, the error I get
is "invalid identifier - table2.emplid"

In English what I want to do is set the fname and lname of table1 to
that of table2 where the emplid's match

The column names given above are the actual column names, so I am
fairly sure these are not reserved words which i know can cause this
error, it must be my awful SQL

Thanks for any assistance

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-08-2008, 01:06 PM
Chris L.
 
Posts: n/a
Default Re: Help with SQL - Update between tables

On 26 jun, 16:20, Problematic coder <gnews...@gmail.com> wrote:
> Here is what I am trying to achieve in sudo code:
>
> update table1 set
> table1.fname = table2.fname,
> table1.lname = table2.lname
> where table1.emplid = table2.emplid;
>
> Now clearly this is wrong since it gives me an error, the error I get
> is "invalid identifier - table2.emplid"
>
> In English what I want to do is set the fname and lname of table1 to
> that of table2 where the emplid's match
>
> The column names given above are the actual column names, so I am
> fairly sure these are not reserved words which i know can cause this
> error, it must be my awful SQL
>
> Thanks for any assistance


Disclaimer: untested

update table1 A set
(fname, lname) = (select B.fname, B.lname
from table2 B
where B.emplid = A.emplid)
where exists
(select * from table2 C where C.emplid = A.emplid);

If you omit the "where exists" part, records that exist only in table1
and not in table2 will be updated to NULL.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-08-2008, 01:06 PM
Problematic coder
 
Posts: n/a
Default Re: Help with SQL - Update between tables

On Jun 26, 12:31 pm, "Chris L." <diver...@uol.com.ar> wrote:
> On 26 jun, 16:20, Problematic coder <gnews...@gmail.com> wrote:
>
>
>
>
>
> > Here is what I am trying to achieve in sudo code:

>
> > update table1 set
> > table1.fname = table2.fname,
> > table1.lname = table2.lname
> > where table1.emplid = table2.emplid;

>
> > Now clearly this is wrong since it gives me an error, the error I get
> > is "invalid identifier - table2.emplid"

>
> > In English what I want to do is set the fname and lname of table1 to
> > that of table2 where the emplid's match

>
> > The column names given above are the actual column names, so I am
> > fairly sure these are not reserved words which i know can cause this
> > error, it must be my awful SQL

>
> > Thanks for any assistance

>
> Disclaimer: untested
>
> update table1 A set
> (fname, lname) = (select B.fname, B.lname
> from table2 B
> where B.emplid = A.emplid)
> where exists
> (select * from table2 C where C.emplid = A.emplid);
>
> If you omit the "where exists" part, records that exist only in table1
> and not in table2 will be updated to NULL.- Hide quoted text -
>
> - Show quoted text -


Perfect, thank you

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:30 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com