我正在尝试将数据插入Oracle数据库中的表中。数据已经存在,但不是所有的数据,我不能只删除数据并重新插入所有数据。是否有方法将数据插入到表中(而不知道丢失了哪些数据)。我的脚本正在运行,但实际上没有数据插入(而且我确实知道有数据丢失。我故意取出数据来测试它的重新插入。)
Insert into item (item, descr)
select distinct a.SUBORD, a.SUBORD_DESCR FROM EXIDE.UDT_BOM a, item b
where b.item = a.subord and not exists
(select b.item from item b, exide.udt_bom a where a.subord = b.ITEM)发布于 2013-09-18 16:17:51
如果我跟踪您正在做的事情,您可以使用语句进行如下操作:
merge into item i
using (select subord, subord_descr from exide.udt_bom) u
on (i.item = u.subord)
when not matched then insert (item, descr) values (u.subord, u.subord_descr);SQL小提琴演示。
这还具有一个优点,即如果udt_bom对现有项有新的描述,您也可以更新item表中的描述:
merge into item i
using (select subord, subord_descr from exide.udt_bom) u
on (i.item = u.subord)
when matched then update set descr = u.subord_descr
when not matched then insert (item, descr) values (u.subord, u.subord_descr);又一把小提琴。
发布于 2013-09-18 16:17:42
对表的引用太多了。使用not exists子句,查询不需要显式联接:
Insert into item(item, descr)
select distinct b.SUBORD, b.SUBORD_DESCR
FROM EXIDE.UDT_BOM b
where not exists (select i.item from item i where b.subord = i.ITEM);如果没有副本,在udt_bom中,我也会去掉distinct。而且,使用表缩写作为别名,而不是使用a、b等无意义的字母时,查询更易读。
https://stackoverflow.com/questions/18876471
复制相似问题