Home » SQL & PL/SQL » SQL & PL/SQL » Update Join.. (Oracle 11.2.0.4/Windows 64 bit)
Update Join.. [message #638926] Thu, 25 June 2015 09:30 Go to next message
sant_new1
Messages: 46
Registered: June 2014
Member
Hi friends,
I need some help with update..

We need to update pc_fname and pc_lname in p_code table from temp_tab table based on the conditions below:
When pc_edid = pi_edid and p_sync = t_sync
where t_ref_no is not null.

create table p_imp (
  pi_edid number(20),
  p_sync varchar2(30)
);
create table temp_tab (
  t_fname varchar2(255),
  t_lname varchar2(255),
  t_sync  varchar2(30),
  t_ref_no varchar2(255)
);
create table p_code(
  pc_fname varchar2(255),
  pc_lname varchar(255),
  pc_edid number(20)
);
 
insert into p_imp values (12,'123456');
insert into p_imp values (30,'7890');
insert into p_imp values (70,'6356');
 
insert into temp_tab values ('John','Smith','123456','500');
insert into temp_tab values ('Mary','Aaron','7890','100');
insert into temp_tab values ('Lewis','Staff','6356','');
 
insert into p_code values ('Tim','Henry',12);
insert into p_code values ('Keith','Bullock',30);
insert into p_code values ('Robert','Clark',70);
 
After the update, p_code should be:
 
'John','Smith',12
'Mary','Aaron',30
'Robert','Clark',70


Please help... Thank you so much
Re: Update Join.. [message #638927 is a reply to message #638926] Thu, 25 June 2015 09:44 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
https://community.oracle.com/thread/3754533
Re: Update Join.. [message #638931 is a reply to message #638927] Thu, 25 June 2015 09:59 Go to previous messageGo to next message
sant_new1
Messages: 46
Registered: June 2014
Member
Yes I posted it there too.

I tried the update below
update p_code pc
set(pc_fname,pc_lname)=
(select t.t_fname,t.t_lname
from temp_tab t , p_imp pi
where
t.t_ref_no is not null
and pc.pc_edid=pi.pi_edid
and t.t_sync=pi.p_sync);

3 rows updated.

But, the last row also got updated with null values...

'John','Smith',12
'Mary','Aaron',30
'','',70

But it should be

'John','Smith',12
'Mary','Aaron',30
'Robert','Clark',70

Basically we would want to update the pc_fname,pc_lname from temp_tab table when t_ref_no is not null. But since we don't have common reference between the tables, we are joining p_imp table with pi_edid=pc_edid and p_sync=t_sync.

Please help..Thank you


Re: Update Join.. [message #638932 is a reply to message #638931] Thu, 25 June 2015 10:12 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You missed a WHERE clause on UPDATE: where (a condition that determines the rows you want to update, for instance EXISTS...)

Re: Update Join.. [message #638933 is a reply to message #638932] Thu, 25 June 2015 10:23 Go to previous message
sant_new1
Messages: 46
Registered: June 2014
Member
Thank Michael. GOt it working now



update p_code pc
set(pc_fname,pc_lname)=
(select t.t_fname,t.t_lname
from temp_tab t , p_imp pi
where
t.t_ref_no is not null
and pc.pc_edid=pi.pi_edid
and t.t_sync=pi.p_sync)
where exists
(select null
from temp_tab t,p_imp pi
where t.t_ref_no is not null
and pc.pc_edid=pi.pi_edid
and t.t_sync=pi.p_sync)

Previous Topic: pl/sql statement ignored
Next Topic: Date format help
Goto Forum:
  


Current Time: Fri Apr 26 12:23:38 CDT 2024