我有以下过程(插入映射):
create or replace procedure p_insert_mapping
(header_id in number,
position in number,
xml_mapping in varchar2,
id out number,
result_code out number
)
is
l_id number;
begin
-- check for errors
if header_id not in (select log_push_readouts_headers.id from log_push_readouts_headers) then
result_code := 9302;
raise_application_error(-RESULT_CODE, 'Foreign key constraint violated for headers');
end if;
-- if there are no errors, do insert
if result_code is null then
-- fetch sequence number
id := mapping_seq.nextval;
insert into log_push_readouts_mappings
(id, position, xml_mapping)
values
(id, position, xml_mapping);
end if;
commit;
end;
在以下行中:
if header_id not in (select log_push_readouts_headers.id from log_push_readouts_headers) then
我需要检查主表中是否存在外键。
该怎么做?
有人可以给我一个例子,说明如何检查外键是否在具有主键的表中吗?
我遇到以下错误:PROCEDURE的编译错误
AMM_MDM.P_INSERT_MAPPING
Error: PLS-00405: subquery not allowed in this context
Line: 12
Text: if header_id not in (select log_push_readouts_headers.id from log_push_readouts_headers) then
Error: PL/SQL: Statement ignored
Line: 12
Text: if header_id not in (select log_push_readouts_headers.id from log_push_readouts_headers) then
答案 0 :(得分:1)
关于编译错误,请参考 this answer
要检查表的某一列上的外键约束,我们可以从all_constrainsts中查询,其中可以检查CONSTRAINT_TYPE。
文档参考:Using a subquery within PLSQL conditional logic; error PLS-00405
答案 1 :(得分:0)
您可以做的是将子查询的匹配计数提取到数字类型变量中,然后在if-else语句中使用它来计算代码流
select count(1) into <some variable> from log_push_readouts_headers a where log_push_readouts_headers.id = header_id
现在在if-else语句中对照您希望的值检查此计数。