1

I have two tables affiliation and customer, in that i have data like this

aff_id  From_cus_id
------  -----------
1       10
2       20
3       30
4       40
5       50 

cust_id   cust_aff_id 
-------   -------
10
20
30
40
50

i need to update data for cust_aff_id column from affiliation table which is aff_id like below

cust_id   cust_aff_id 
-------   -------
10        1
20        2
30        3
40        4
50        5

could u please give reply if anyone knows......

0

3 Answers 3

1

Oracle doesn't have an UPDATE with join syntax, but you can use a subquery instead:

UPDATE customer
SET customer.cust_aff_id =
(SELECT aff_id FROM affiliation WHERE From_cus_id = customer.cust_id)
Sign up to request clarification or add additional context in comments.

2 Comments

Of course Oracle does have UPDATE with join syntax - but that comes with restrictions.
@mathguy Not that I know of. Maybe you could post an answer showing this.
1
merge into customer t2 
  using affiliation t1  on (t1.From_cus_id =t2.cust_id )
  WHEN MATCHED THEN
  update set t2.cust_aff_id  = t1.aff_id
  ;

Comments

0

Here is an update with join syntax. This, quite reasonably, works only if from_cus_id is primary key in the first table and cust_id is foreign key in the second table, referencing the first table. Without these conditions, the requirement doesn't make much sense in the first place anyway... but Oracle requires that these constraints be stated explicitly in the tables. This is also reasonable on Oracle's part IMO.

update 
    ( select t1.aff_id, t2.cust_aff_id 
      from affiliation t1 join customer t2 on t2.cust_id = t1.from_cus_id)  j 
set j.cust_aff_id = j.aff_id;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.