VastbaseG100

基于openGauss内核开发的企业级关系型数据库。

Menu

错误码

功能描述

支持在PL/pgSQL的异常处理中捕获invalid_number、dup_val_on_index异常。

错误码SQLSTATE值错误码含义
P0025INVALID_NUMBER,无实际含义,需自主声明后使用。
23505DUP_VAL_ON_INDEX,违反唯一约束。

注意事项

仅Vastbase V2.2 Build 10(Patch No.17)及以后版本支持此功能。

示例

示例1: 匿名块中自定义invalid_number异常场景并触发。

set serveroutput on;  --允许将dbms_output.put_line的输出信息输出至vsql的命令界面的屏幕上
declare
 v_var1 number :=2;
 v_var2 number :=1;
begin
  if v_var1 > v_var2 then
     raise invalid_number;
  end if;
exception
  when invalid_number then 
    DBMS_OUTPUT.PUT_LINE('输入不合法!');
end;
/

返回结果如下:

输入不合法!
ANONYMOUS BLOCK EXECUTE

示例2: 错误场景下触发dup_val_on_index异常。

set serveroutput on;  --允许将dbms_output.put_line的输出信息输出至vsql的命令界面的屏幕上
declare
v_error_code NUMBER;
v_error_message VARCHAR2(4000);
begin
create table tb1(id int primary key);
insert into tb1 values(1);
insert into tb1 values(1);
exception when dup_val_on_index then
v_error_code := SQLCODE;
v_error_message := SQLERRM;
DBMS_OUTPUT.PUT_LINE('Error code: ' || v_error_code);
DBMS_OUTPUT.PUT_LINE('Error message: ' || v_error_message);
raise info '违反唯一约束';
end;
/

返回结果如下:

NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "tb1_pkey" for table "tb1"
CONTEXT:  SQL statement "create table tb1(id int primary key)"
PL/pgSQL function inline_code_block line 5 at SQL statement
Error code: 23505
Error message: duplicate key value violates unique constraint "tb1_pkey"
INFO:  违反唯一约束
ANONYMOUS BLOCK EXECUTE