SQL 插入过程:将值插入表中并控制另一个表中是否存在值



我没有什么问题。我正在尝试将值插入表中。这是有效的。但我想控制值id_trainer是否存在于另一个表中。我想要这个 ->执行插入俱乐部(1, 5, 'someName'(;-> 如果表训练器中不存在id_trainer 5,则过程会给我有关此的消息。(我试图把它翻译成英语液化天然气,所以你可以找到一些克错误(

create or replace procedure insertClub
(id_club in number, id_trainer in number, clubName in varchar2)
is
begin
declare counter number;
select count(*) into counter from trianer tr where tr.id_trainer = id_trainer;
if counter = 0 then
DBMS_OUTPUT.PUT_LINE('Trainer with this ID not exists');
end if;
insert into club values(id_club, id_trainer, clubName);
exception
  when dup_val_on_index then
DBMS_OUTPUT.PUT_LINE('Dup ID');
end;
/

过程中存在一些错误。请运行以下代码来创建过程:

create or replace procedure insertClub
(id_club in number, id_trainer in number, clubName in varchar2)
is
 counter number;
begin
select count(*) into counter from trianer tr where tr.id_trainer = id_trainer;
if counter = 0 then
DBMS_OUTPUT.PUT_LINE('Trainer with this ID not exists');
end if;
insert into club values(id_club, id_trainer, clubName);
exception
  when dup_val_on_index then
DBMS_OUTPUT.PUT_LINE('Dup ID');
end;
/