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!

Update to change to another lookup table

fatehcis-JavaNetOct 23 2012 — edited Oct 24 2012
Hello,
I have two tables:
create table units (id number primary key , agent number, 
constraint agnent_fk foreign key (agent) references agent_lookup (id) )
create table agent_lookup (id number primary key , agent_name varchar2 (40))
INSERT INTO AGENT_LOOKUP  (ID, AGENT_NAME) VALUES (1, 'X1');
INSERT INTO AGENT_LOOKUP  (ID, AGENT_NAME) VALUES (2, 'X2');
INSERT INTO AGENT_LOOKUP  (ID, AGENT_NAME) VALUES (3, 'X3');
INSERT INTO UNITS  (ID, AGENT) VALUES (100, 1);
INSERT INTO UNITS  (ID, AGENT) VALUES (101, 2);
INSERT INTO UNITS  (ID, AGENT) VALUES (102, 3);
I want to make units table uses user_lookup table instead of agent_lookup table; where user_lookup contains the same names that exist on agent_lookup, but with different IDs.
create table user_lookup ( id number primary key , user_name varchar2 (40))
INSERT INTO USER_LOOKUP  (ID, USER_NAME) VALUES (10, 'X1');
INSERT INTO USER_LOOKUP  (ID, USER_NAME) VALUES (20, 'X2');
INSERT INTO USER_LOOKUP  (ID, USER_NAME) VALUES (30, 'X3');
So, I need to update the table units and SET the value of Agent to the ID column of the user_lookup table .

How can I do it ??

I thought of this, but sure it will not work:
update units ut set ut.agent = (select u.id from user_lookup u where agent_lookup.agent_name = u.user_name )
where ut.agent = agent_lookup.id
Regards,
Fateh
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 21 2012
Added on Oct 23 2012
7 comments
282 views