使用子查询将oracle插入到列中

使用子查询将oracle插入到列中,oracle,insert,subquery,Oracle,Insert,Subquery,我想在表中的列中插入数据 Table a ID col1 col2 1 A null 2 B null Table b ID col1 1 C 2 D 预期成果: Table A ID col1 col2 1 A C 2 B D 我试过这个: insert into tableA (col2) select b.col1 from tableB b , tableA a where b.id = a.i

我想在表中的列中插入数据

Table a
ID  col1  col2
1    A    null  
2    B    null

Table b
ID  col1 
1    C
2    D
预期成果:

Table A
ID   col1  col2
1     A     C
2     B     D
我试过这个:

insert into tableA (col2) 
select b.col1
from tableB b , tableA a
where b.id = a.id
我收到了

0 row inserted.
如何将B中的col1插入A中的col2,以匹配“id”列


谢谢。

您想做的事情不需要子查询。我不是表a、表b的超级粉丝,试试这个:

update a
set col2 = b.col1
from tableB b 
join tableA a
on a.id = b.id

基于联接插入时必须使用Merge语句。 表tableA col2中也已存在,但若要在join中插入值,则必须更新该列

并入表格 使用表b on(b.id=a.id) 匹配时 然后
更新集a.col2=b.col1

您需要做的是更新,而不是插入。