我有以下sql查询来插入表中的记录。问题是当没有select查询的记录时,我仍然想要一个带有序列号的记录,null,'3'值被插入。如果没有找到select查询的记录,则不会插入任何内容。我怎么能这样做?
insert into test_details(seqnbr,trepid,type)
select '&seqid'
,REP_ID
,'3'
FROM ref_details
WHERE REP_ID >13;
答案 0 :(得分:4)
一种方法是
insert into test_details(seqnbr,trpid,type)
select '&seqid',rep_id,'3' from ref_details where rep_id>13
union all select '&seqid',null,'3'
from dual where not exists(
select 1 from ref_details where rep_id>13)
答案 1 :(得分:3)
要填补空白,您需要create a list of sequencial values:
INSERT INTO TEST_DETAILS
(seqnbr, trpid, type)
SELECT '&seqid', rd.rep_id, '3'
FROM (SELECT LEVEL + 13
FROM DUAL
CONNECT BY LEVEL <= 13) x
LEFT JOIN REF_DETAILS rd ON rd.rep_id = x.level
AND rd.rep_id > 13
...然后LEFT JOIN到可能有间隙的表格。