错误俘获处理

最近更新时间: 2024-06-12 15:06:00

postgres=# CREATE TABLE t_exception (id integer not null,nc text);       
CREATE TABLE
postgres=# create unique index t_exception_id_uidx on t_exception using btree(id);
CREATE INDEX
postgres=# CREATE OR REPLACE FUNCTION f27(a_id integer,a_nc text) RETURNS TEXT AS
postgres-# $$    
postgres$# BEGIN           
postgres$#     INSERT INTO t_exception VALUES(a_id,a_nc);
postgres$#     RETURN '';
postgres$#     EXCEPTION WHEN OTHERS THEN     
postgres$#     RETURN '执行出错';   
postgres$# END;
postgres$# $$
postgres-# LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# SELECT f27(1,'TBase');
 f27 
----- 
(1 row)
postgres=# SELECT f27(1,'TBase');
   f27    
----------
 执行出错
(1 row)