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

时间:2017-05-16 15:45:24

标签: sql oracle

我没什么问题。我试图将值插入表中。这很有效。但我想控制值id_trainer是否存在于另一个表中。我想要这个 - >执行insertClub(1,5和' someName'); - >如果表Trainer中不存在id_trainer 5,则程序会给出关于此的消息。 (我试图把它翻译成eng.lng。,所以你可以找到一些gramm。错误)

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;
/

1 个答案:

答案 0 :(得分:0)

程序中有一些错误。请在代码下面运行以创建程序:

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;
/