我有一个小问题,假设我有一个一个有 5 列的表,我使用另一个有 4 列的表的 MINUS
插入到该表中.
当我执行该语句时,在第五列中插入 (null) 或“”值时出现问题:
选择的列数无效
(我从中减去的表没有第五列)
这是我用于插入语句的代码
INSERT INTO my_table(SELECT col1, col2, col3, col4 FROM that_table
MINUS SELECT col1, col2, col3, col4 FROM my_table);
这段代码有效,如果我不创建第五列,它会复制我需要的任何内容,有没有办法将另一个表中的值与 my_table 中的第五列一起插入到我的表中?
谢谢
最佳答案
假设我理解您的问题,您希望从 that_table
中选择所有记录,并将它们插入到 my_table
中,其中前 4 列不存在,使用 null
作为第五列的值?如果是这样,您可以使用不存在
:
insert into my_table (col1, col2, col3, col4, col5)
select col1, col2, col3, col4, null
from that_table tt
where not exists (
select 1
from my_table mt
where tt.col1 = mt.col1 and tt.col2 = mt.col2
and tt.col3 = mt.col3 and tt.col4 = mt.col4
)
您还应该能够使用减号
,只需确保具有相同的列数和相同的数据类型即可:
insert into my_table (col1, col2, col3, col4, col5)
select col1, col2, col3, col4, null
from that_table
minus
select col1, col2, col3, col4, null
from my_table
关于sql - PL/SQL插入一个表减去另一个表但同时插入另一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23862519/