Home » SQL & PL/SQL » SQL & PL/SQL » update a table based on another table with no specific order (Oracle 10g)
update a table based on another table with no specific order [message #622630] Thu, 28 August 2014 03:55 Go to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Hi All,

I want to update one tables column based on other tables column with no specific order.
So the comm column of emp must be updated with sal column from emp1 .

         
create table emp1 as select sal from emp;--only sal column suppose one table is having
         
update emp set comm=(select sal from emp1) ;


Regards,
Nathan
Re: update a table based on another table with no specific order [message #622631 is a reply to message #622630] Thu, 28 August 2014 04:00 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
So you want to set comm to a random sal?
Re: update a table based on another table with no specific order [message #622632 is a reply to message #622631] Thu, 28 August 2014 04:02 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Thanks for your reply and Yes I want to update comm column .
Re: update a table based on another table with no specific order [message #622634 is a reply to message #622632] Thu, 28 August 2014 04:05 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
It's impressive that you've managed to write an answer that only answers half of my very simple question. Try answering the other half as well.
Re: update a table based on another table with no specific order [message #622635 is a reply to message #622630] Thu, 28 August 2014 04:08 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Looking at the two queries you posted, i think you need this :

update emp set comm=sal;


Why two tables?

[Updated on: Thu, 28 August 2014 04:09]

Report message to a moderator

Re: update a table based on another table with no specific order [message #622638 is a reply to message #622635] Thu, 28 August 2014 04:14 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Both the answers are apropriate of yours.
Think emp1 is having different column not the sal.
But I need the data from the table to update another table with no driven(intermediate/join) column.
The insert .. select seems to be more easy than update .

update emp f set sal= to_number(rownum||'00');
or 
update emp set comm=sal;

[Updated on: Thu, 28 August 2014 04:18]

Report message to a moderator

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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 #622644 is a reply to message #622642] Thu, 28 August 2014 04:32 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
*sigh* - previous post is rendered meaningless by OP updating his previous post.

That code is likely to update nothing.
Instead of posting code that doesn't work, describe the rules you want to use to update the data in words.
Re: update a table based on another table with no specific order [message #622645 is a reply to message #622644] Thu, 28 August 2014 04:41 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member


Sorry I mistakenly have posted not working code.
I want to update emp table comm column with emp1 sal column with no duplication but can be any specific order.
Means 14 rows should get updated to emp table with no repeat.
Re: update a table based on another table with no specific order [message #622646 is a reply to message #622641] Thu, 28 August 2014 04:41 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
sss111ind wrote on Thu, 28 August 2014 14:54
second is what cookiemonster might have thought .


Shocked Hope you made CM's day!


sss111ind wrote on Thu, 28 August 2014 14:44
update another table with no driven(intermediate/join) column.


and

Quote:
Now I am doing following way.
WHERE empno=i.empno


are contradicting to each other.
Re: update a table based on another table with no specific order [message #622647 is a reply to message #622646] Thu, 28 August 2014 04:44 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Make the where clause of your update be:
empno=j.empno
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 Go to previous messageGo to next message
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 #622651 is a reply to message #622648] Thu, 28 August 2014 04:55 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

update emp e set comm=(select sal from emp1 e1 where e1.empno=e.empno);


Re: update a table based on another table with no specific order [message #622687 is a reply to message #622651] Thu, 28 August 2014 09:05 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Michel, actually OP is insisting on an impossible rule :

Quote:
update another table with no driven(intermediate/join) column
Re: update a table based on another table with no specific order [message #622690 is a reply to message #622687] Thu, 28 August 2014 09:48 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Which is for me a complete meaningless sentence and is completely outside his code which manually DOES a join : "WHERE empno=i.empno"

Re: update a table based on another table with no specific order [message #622691 is a reply to message #622690] Thu, 28 August 2014 09:53 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Yes, it's not just meaningless but impossible in any ORDBMS platform. It violates the basic principle. And I already told OP in above posts that his statements are contradicting. But, those were unaddressed by him Sad
Re: update a table based on another table with no specific order [message #622692 is a reply to message #622690] Thu, 28 August 2014 09:53 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
That's not a join, it's a predicate, as i is a for loop row variable.
That code updates each row from the outer loop once with one of the rows from the inner loop.
Re: update a table based on another table with no specific order [message #622693 is a reply to message #622691] Thu, 28 August 2014 09:57 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
CM, Perhaps Michel quoted the plsql code, but I believe we(I and Michel) are discussing on the SQL which Michel posted. May be Michel could confirm it.
Re: update a table based on another table with no specific order [message #622694 is a reply to message #622691] Thu, 28 August 2014 09:58 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Why do you waste time responding when OP refuses to comply with Posting Guidelines?
Re: update a table based on another table with no specific order [message #622696 is a reply to message #622694] Thu, 28 August 2014 10:05 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Yes I leave this topic that is going nowhere.

Re: update a table based on another table with no specific order [message #622698 is a reply to message #622694] Thu, 28 August 2014 10:07 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
BlackSwan wrote on Thu, 28 August 2014 20:28
Why do you waste time responding when OP refuses to comply with Posting Guidelines?


Well, I was responding only to Michel. Now I am out of it, as I now find the discussion directionless.
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 Go to previous message
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

Previous Topic: Analytical Query AVG for different days in same sql
Next Topic: Invalid reference to variable, ORA-22806: not an object or REF
Goto Forum:
  


Current Time: Fri Apr 26 15:13:35 CDT 2024