我的要求是编写一个在COUNTRIES
表中添加值的过程。但是,首先它应该检查对应的值是否存在于另一个表中,REGIONS
因为它是一个外键。仅当值存在时才允许插入到COUNTRIES
表中。第二个要求是我不应该按原样country_id
在COUNTRIES
表中有重复的country_id
值PK
。我写了这段代码,但它抛出了异常:
PLS-00103 Encountered the symbol "WHEN" when expecting one of the following:
begin case
PLS-00103:Encountered the symbol "WHEN" when expecting one of the following: ERROR
我的代码是:
create or replace procedure addi5 (c_cntry_id in out countries.country_id%type,
c_cntr_name in countries.country_name%type,
c_rgn_id in countries.region_id%type)
is
region_exists pls_integer;
begin
begin
select 1 into region_exists
from regions r
where r.region_id = c_rgn_id;
exception
when no_data_found then
region_exists := 0;
DBMS_OUTPUT.PUT_LINE('Region not present '||sqlerrm);
end;
BEGIN
INSERT INTO countries(COUNTRY_ID, COUNTRY_NAME,REGION_ID)
values (c_cntry_id, c_cntr_name,c_rgn_id);
EXCEPTION
WHEN dup_val_on_index THEN
c_cntry_id := null;
DBMS_OUTPUT.PUT_LINE('Already present');
END;
if region_exists = 1 then
insert into countries(country_id, country_name,region_id)
values (c_cntry_id, c_cntr_name, c_rgn_id);
DBMS_OUTPUT.PUT_LINE('Inserted');
ELSE
WHEN dup_val_on_index
THEN
c_cntry_id := null;
DBMS_OUTPUT.PUT_LINE('Already present');
END IF;
end addi5;
/
有人可以指导我做错了什么吗?或者如何编写或检查这两个条件?