|
|
|
|
|
|
Re: update a table based on another table with no specific order [message #622640 is a reply to message #622638] |
Thu, 28 August 2014 04:21 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
You are changing the requirement now. Earlier you said you need to update a column from another table's column values. But this query defeats the purpose :
sss111ind wrote on Thu, 28 August 2014 14:44
update emp f set sal= to_number(rownum||'00');
So, you need to fix to what you want.
sss111ind wrote on Thu, 28 August 2014 14:44
The insert .. select seems to be more easy than update .
What do you mean? Please SHOW.
[Updated on: Thu, 28 August 2014 04:23] Report message to a moderator
|
|
|
Re: update a table based on another table with no specific order [message #622641 is a reply to message #622640] |
Thu, 28 August 2014 04:24 |
|
sss111ind
Messages: 634 Registered: April 2012 Location: India
|
Senior Member |
|
|
The two queries which I have pasted one is yours and second is what cookiemonster might have thought .
But still I want the same thing to update based on another table.
Now I am doing following way.
DECLARE
II NUMBER:=0;
JJ NUMBER:=0;
BEGIN
FOR I IN ( select empno FROM emp1) LOOP-- refering table
JJ :=0;
FOR J IN (select empno FROM emp) loop--updating table
if II=JJ then
update emp set comm= sal WHERE empno=i.empno;
EXIT;
end if;
JJ:=JJ+1;
end loop;
II:=II+1;
END LOOP;
END;
[Updated on: Thu, 28 August 2014 04:29] Report message to a moderator
|
|
|
Re: update a table based on another table with no specific order [message #622642 is a reply to message #622641] |
Thu, 28 August 2014 04:29 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
sss111ind wrote on Thu, 28 August 2014 10:24
But still I want the same thing to update based on another table.
That's not going to get any more meaningful no matter how many times you repeat it.
There are lots of ways to update something based on data from another table.
When we write code we have a specific set of rules in mind for updating the data.
You need to tell us the rules you want to apply.
|
|
|
|
|
|
|
Re: update a table based on another table with no specific order [message #622648 is a reply to message #622646] |
Thu, 28 August 2014 04:44 |
|
sss111ind
Messages: 634 Registered: April 2012 Location: India
|
Senior Member |
|
|
Thank you,Can we update a table with random value from another table.As follows
update emp set comm=rownum;--happening
update emp set comm=(select rownum from dual);--not happening
Actually it should be
DECLARE
II NUMBER:=0;
JJ NUMBER:=0;
BEGIN
FOR I IN ( select empno FROM emp) LOOP --updating table
JJ :=0;
FOR J IN (select sal FROM emp1) loop-- refering table
if II=JJ then
update emp set comm= j.sal WHERE empno=i.empno;
EXIT;
end if;
JJ:=JJ+1;
end loop;
II:=II+1;
END LOOP;
END;
But can we do in a single query with out plsql.
[Updated on: Thu, 28 August 2014 04:49] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|
Re: update a table based on another table with no specific order [message #622699 is a reply to message #622696] |
Thu, 28 August 2014 10:19 |
|
sss111ind
Messages: 634 Registered: April 2012 Location: India
|
Senior Member |
|
|
If we do like as follows then I think we are getting.
update emp set COMM='';
drop table test_emp;
create table test_emp as select sal from emp3;
select * from test_emp;
alter table test_emp add (rn number);
update test_emp set rn=rownum;
create or replace function test_func(p_rn number) return number
is
l_data number;
begin
select sal into l_data from test_emp where rn=p_rn;
return l_data;
end;
update emp set comm=test_func(rownum) ;
commit;
select * from emp;
[Updated on: Thu, 28 August 2014 10:20] Report message to a moderator
|
|
|