I have two tables I want to update table b in column bcnt by the value that in column acnt in table A where the worlds column in table A match words column in table B, and id in table B match id in table A.
create table a
(
id number(9),
words varchar2(2),
acnt number(9)
);
insert into a values(1,'Cairo',20);
insert into a values(1,'UK',10);
insert into a values(2,'KL',2);
insert into a values(2,'Cairo',2);
insert into a values(2,'London',30);
insert into a values(3,'Cairo',5);
insert into a values(4,'KSA',15);
create table b
(
id number(2),
words varchar2(20),
bcnt number
);
insert into b values(1,'Cairo',null);
insert into b values(1,'UK',null);
insert into b values(2,'KL',null);
insert into b values(2,'Cairo',null);
insert into b values(3,'Cairo',null);
insert into b values(4,'KSA',null);
I used this SQL code but it is not correct.
update b
set bcnt = (select acnt
from a
where a.id = b.id and a.words = b.words);
Expected results:
1 cairo 20
1 uk 10
2 kl 2
2 cairo 5
4 ksa 12
The SQL shows me the following
SQL> /
ID WORDS BCNT
---------- -------------------- ----------
1 Cairo
1 UK 10
2 KL 2
2 Cairo
3 Cairo
4 KSA
6 rows selected.
SQL>
Is the problem that your SQL uses
wordinstead ofwordsas in the table definition?Also, your data types are not correct in the two tables. Your create table statements should be consistent:
What is happening is that the values longer than 2 characters in the first table are being truncated to two characters. So, instead of ‘Cairo’ the value is ‘Ca’ (to fit in varchar2(2)). As a result, you have no match in the join.