[英]My PL/SQL procedure exception doesn't seem to work
I have a database with continents and I wanted to create a procedure that added a new continent to the database, with exceptions.我有一个包含大陆的数据库,我想创建一个过程,将新大陆添加到数据库中,但有例外。 I tried executing the procedure inside my package GEST_GEO by creating an already existing continent (Asia) and my exception was not raised.我尝试通过创建一个已经存在的大陆(亚洲)来执行我的包 GEST_GEO 中的过程,但没有引发我的异常。 Does anyone have any idea that to why it doesn't work ?有没有人知道为什么它不起作用? (nom is name) (nom 是名字)
Here is my code :这是我的代码:
CREATE OR REPLACE PACKAGE GEST_GEO AS
Procedure ADDCONTINENT(pnom continent.nom%TYPE, psuperficie continent.superficie%TYPE);
END GEST_GEO;
/
CREATE OR REPLACE PACKAGE BODY GEST_GEO AS
-- ADDCONTINENT
Procedure ADDCONTINENT(pnom CONTINENT.NOM%TYPE, psuperficie CONTINENT.SUPERFICIE%TYPE) IS
BEGIN
INSERT INTO CONTINENT VALUES (pnom, psuperficie);
COMMIT;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN DBMS_OUTPUT.PUT_LINE('The continent already exists');
WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE(SQLCODE || '–' || SQLERRM);
END ADDCONTINENT;
END GEST_GEO;
/
execute GEST_GEO.ADDCONTINENT('Asia',99380);
When executing it it just tells me PL/SQL procedure successfully completed
执行时它只是告诉我PL/SQL procedure successfully completed
Thank you谢谢
DUP_VAL_ON_INDEX
presumes that there's a primary or unique key, or unique index enforced on that column. DUP_VAL_ON_INDEX
假定有一个主键或唯一键,或对该列强制执行的唯一索引。 If there's none, Oracle doesn't know that there's anything "duplicated" so it inserts a row.如果没有,Oracle 不知道有任何“重复”的东西,所以它插入一行。
Therefore, create something of previously mentioned (keys/indexes).因此,创建一些前面提到的(键/索引)。
Here's an example: table first (note the primary key constraint in line #2):这是一个示例:表优先(注意第 2 行中的主键约束):
SQL> create table continent
2 (nom varchar2(30) constraint pk_cont primary key,
3 superficie number
4 );
Table created.
Procedure: note程序:注意
INSERT
statement第 5 行 - 始终在INSERT
语句中包含目标列名RAISE
the error instead RAISE
错误So:所以:
SQL> create or replace procedure addcontinent
2 (pnom continent.nom%type, psuperficie continent.superficie%type)
3 is
4 begin
5 insert into continent (nom, superficie) values (pnom, psuperficie);
6 exception
7 when dup_val_on_index then
8 raise_application_error(-20001, 'The continent already exists');
9 when others then
10 raise;
11 end addcontinent;
12 /
Procedure created.
Testing:测试:
SQL> exec addcontinent('Asia', 99380);
PL/SQL procedure successfully completed.
SQL> exec addcontinent('Asia', 99380);
BEGIN addcontinent('Asia', 99380); END;
*
ERROR at line 1:
ORA-20001: The continent already exists
ORA-06512: at "SCOTT.ADDCONTINENT", line 8
ORA-06512: at line 1
SQL> select * from continent;
NOM SUPERFICIE
------------------------------ ----------
Asia 99380
SQL>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.