【发布时间】:2015-09-19 15:17:24
【问题描述】:
我正在尝试将单个记录插入到表中并返回通过 ASP.net/Visual Studio 添加到记录中的序列号。但是,我收到了上面提到的错误。最初我认为我的错误是因为它认为我可能会返回多个记录,但即使在重写了几种方法之后,错误仍然存在。关于这个主题存在多个帖子,但它们似乎都围绕着插入多条记录的可能性。
我怀疑因为我正在使用“select... from dual”,它仍然认为我可以插入多个记录。我显然不需要“select...from dual”,只是我想使用 WHERE 子句来保证目标表中不存在该记录。
任何帮助或建议将不胜感激。谢谢。
INSERT INTO blatchildren
(blatranscriptid, childactivityid, enrollmentDate, enrollmentStatus)
SELECT 2,
'cours000000000004981',
to_date('1/1/2015 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'),
'E'
from dual
where 'cours000000000004981' not in (select childactivityid from blatchildren)
returning id
into :identity
为了测试代码,我一直在 PL/SQL Developer 中运行以下代码:
declare identity number(2);
begin
INSERT INTO blatchildren
(blatranscriptid, childactivityid, enrollmentDate, enrollmentStatus)
VALUES( 2,
'cours000000000004981',
to_date('1/1/2015 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'),
'E')
returning id
into identity;
end;
【问题讨论】:
-
INSERT..SELECT FROM DUAL..WHERE 不要混合,你不想重复使用唯一键,即使没有 from dual 仍然没有意义
-
@wero - 是的,多次。
-
@LukasEder - 我正在使用 PL/SQL 工具进行测试,但在 Visual Studio 中使用。
-
您愿意在没有 where 子句的情况下运行此 SQL(从 blatchildren 中选择 childactivityid)。相反,如果您运行 select childiactivity from blatchchildren where childactivityid='someid' 然后使用 0 的 sql rowcnt 来决定插入,或者 1 - 不插入。