关于sql查询

发布于 2024-09-08 09:13:07 字数 255 浏览 3 评论 0原文

我有下面的 sql 查询来从表中插入记录。问题是,当没有选择查询的记录时,我仍然想要插入序列号、null、'3' 值的记录。当没有找到选择查询的记录时,不会插入任何内容。我该怎么做?

insert into test_details(seqnbr,trepid,type)
select '&seqid'
      ,REP_ID
      ,'3'
  FROM ref_details
 WHERE REP_ID >13;

I have the below sql query to insert the records from a table. The problem is when there is no records for select query i still want an record with the sequence number,null,'3' value inserted. Nothing get's inserted when there is no record found for select query. how can i do it?

insert into test_details(seqnbr,trepid,type)
select '&seqid'
      ,REP_ID
      ,'3'
  FROM ref_details
 WHERE REP_ID >13;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨落星ぅ辰 2024-09-15 09:13:07

一种方法是

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)

One way would be

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)
终止放荡 2024-09-15 09:13:07

Oracle 9i+:

要填补空白,您需要 创建一个序列值列表

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 到可以有间隙的表。

Oracle 9i+:

To fill in gaps, you need to 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

...then LEFT JOIN to the table that can have gaps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文