postgreSQL PL/SQL编程学习笔记(四)
Errors and Messages
1. Reporting Errors and Messages
Use the RAISE statement to report messages and raise errors.
RAISE [ level ] ’format’ [, expression [, ... ]] [ USING option = expression [, ... ] ];
RAISE [ level ] condition_name [ USING option = expression [, ... ] ];
RAISE [ level ] SQLSTATE ’sqlstate’ [ USING option = expression [, ... ] ];
RAISE [ level ] USING option = expression [, ... ];
RAISE ;
The level option specifies the error severity. Allowed levels are DEBUG, LOG, INFO, NOTICE, WARNING, and EXCEPTION, with EXCEPTION being the default.
EXCEPTION raises an error (which normally aborts the current transaction); the other levels only generate messages of different priority levels.
Whether messages of a particular priority are reported to the client, written to the server log, or both is controlled by the log_min_messages and client_min_messages configuration variables.
After level if any, you can write a format (which must be a simple string literal, not an expression).
The format string specifies the error message text to be reported. The format string can be followed by optional argument expressions to be inserted into the message.
Inside the format string, % is replaced by the string representation of the next optional argument’s value.
Write %% to emit a literal %. The number of arguments must match the number of % placeholders in the format string, or an error is raised during the compilation of the function.
In this example, the value of v_job_id will replace the % in the string:
RAISE NOTICE ’Calling cs_create_job(%)’, v_job_id;
You can attach additional information to the error report by writing USING followed by option = expression items. Each expression can be any string-valued expression.
The allowed option key words are:
MESSAGE
Sets the error message text. This option can’t be used in the form of RAISE that includes a format
string before USING.
DETAIL
Supplies an error detail message.
HINT
Supplies a hint message.
ERRCODE
Specifies the error code (SQLSTATE) to report, either by condition name or directly as a five-character SQLSTATE code.
COLUMN
CONSTRAINT
DATATYPE
TABLE
SCHEMA
Supplies the name of a related object.
This example will abort the transaction with the given error message and hint:
RAISE EXCEPTION ’Nonexistent ID --> %’, user_id
USING HINT = ’Please check your user ID’;
These two examples show equivalent ways of setting the SQLSTATE:
RAISE ’Duplicate user ID: %’, user_id USING ERRCODE = ’unique_violation’;
RAISE ’Duplicate user ID: %’, user_id USING ERRCODE = ’23505’;
There is a second RAISE syntax in which the main argument is the condition name or SQLSTATE to be reported, for example:
RAISE division_by_zero;
RAISE SQLSTATE ’22012’;
In this syntax, USING can be used to supply a custom error message, detail, or hint.
Another way to do the earlier example is
RAISE unique_violation USING MESSAGE = ’Duplicate user ID: ’ || user_id;
Still another variant is to write RAISE USING or RAISE level USING and put everything else into the USING list.
The last variant of RAISE has no parameters at all. This form can only be used inside a BEGIN block’s EXCEPTION clause; it causes the error currently being handled to be re-thrown.
If no condition name nor SQLSTATE is specified in a RAISE EXCEPTION command, the default is to use RAISE_EXCEPTION (P0001).
If no message text is specified, the default is to use the condition name or SQLSTATE as message text.
Note: When specifying an error code by SQLSTATE code, you are not limited to the predefined error codes, but can select any error code consisting of five digits and/or upper-case ASCII letters, other than 00000.
It is recommended that you avoid throwing error codes that end in three zeroes, because these are category codes and can only be trapped by trapping the whole category.
2. Checking Assertions
The ASSERT statement is a convenient shorthand for inserting debugging checks into PL/pgSQL functions.
ASSERT condition [ , message ];
The condition is a Boolean expression that is expected to always evaluate to true; if it does, the ASSERT statement does nothing further. If the result is false or null, then an ASSERT_FAILURE exception is raised. (If an error occurs while evaluating the condition, it is reported as a normal error.)
If the optional message is provided, it is an expression whose result (if not null) replaces the default error message text “assertion failed”, should the condition fail. The message expression is not evaluated in the normal case where the assertion succeeds.
Testing of assertions can be enabled or disabled via the configuration parameter plpgsql.check_asserts, which takes a Boolean value; the default is on. If this parameter is off then ASSERT statements do nothing.
Note that ASSERT is meant for detecting program bugs, not for reporting ordinary error conditions. Use the RAISE statement, described above, for that
postgreSQL PL/SQL编程学习笔记(四)的更多相关文章
- postgreSQL PL/SQL编程学习笔记(二)
Control Structures of PL/SQL Control structures are probably the most useful (and important) part of ...
- postgreSQL PL/SQL编程学习笔记(六)——杂七杂八
1 PL/pgSQL Under the Hood This part discusses some implementation details that are frequently import ...
- postgreSQL PL/SQL编程学习笔记(五)——触发器(Triggers)
Trigger Procedures PL/pgSQL can be used to define trigger procedures on data changes or database eve ...
- postgreSQL PL/SQL编程学习笔记(三)——游标(Cursors)
Cursors Rather than executing a whole query at once, it is possible to set up a cursor that encapsul ...
- postgreSQL PL/SQL编程学习笔记(一)
1.Structure of PL/pgSQL The structure of PL/pgSQL is like below: [ <<label>> ] [ DECLARE ...
- PL/SQL编程基础(四):程序控制(IF,CASE,FOR,LOOP,WHILE LOOP,EXIT,CONTINUE)
程序控制 程序结构有分支结构与循环结构: 分支结构语法:IF.CASE: 循环结构:FOR.WHILE LOOP:先执行再判断,至少执行一次: WHILE LOOP:先判断再执行,如果不满足条件,就不 ...
- MySql-Mysql技术内幕~SQL编程学习笔记(N)
1._rowid 类似Oracle的rowid mysql> ; +-------+----+----------------+-------------+---------------+--- ...
- MySql-Mysql技术内幕~SQL编程学习笔记(1)
1.MySQL的历史,一些相关概念. 2.MySQL数据类型 *通常一个页内可以存放尽可能多的行,那么数据库的性能就越好,选择一个正确的数据类型至关重要. 1>UNSIGNED类型: 将数字类型 ...
- 【Linux_Shell 脚本编程学习笔记四、监控系统内存并报警企业案例脚本】
前置知识:awk 参考学习博客:https://www.cnblogs.com/bugingcode/p/8287914.html awk 'BEGIN{ commands } pattern{ co ...
随机推荐
- HDFS的介绍
设计思想 分而治之:将大文件.大批量文件,分布式存放在大量服务器上,以便于采取分而治之的方式对海量数据进行运算分析: 在大数据系统中作用:为各类分布式运算框架(如:mapreduce,spark,te ...
- sql代码段添加数据
declare @i int,@index int set @i=1 set @index=0 while @i<1000000 begin set @inde ...
- html5调用本机摄像头兼容谷歌浏览器高版本,谷歌浏览器低版本,火狐浏览器
做这个功能的时候在网上查了一些资料,代码如下,在这个代码在谷歌浏览器46版本是没问题的,在火狐浏览器也行,但是在谷歌浏览器高版本下是不兼容的 <div id="body"&g ...
- 关于linux创建用户的有趣事情!
小博主今天接受了一项光荣的任务!为什么说是光荣任务呢?因为这个任务是需要创建一个linux用户!!! 肯定有小伙伴要嘲笑了!创建一个用户有什么难的啊! 对!这个并不难,即使是小白也能轻松应对! 但是! ...
- HBase 官方文档中文版
地址链接: http://abloz.com/hbase/book.html 里面包含基本的API和使用说明
- 自定义type
- svn add文件名包含@符号的解决方案
[svn add文件名包含@符号的解决方案] 在iOS开发过程中,代码得用SVN管理起来,但是遇到这么个问题:Default@2x.png文件svn add不成功,总提示找不到这个文件. 结果查了查资 ...
- 查看window下默认ORACLE_SID
Configuration and Migration Tools-----Configuration and Migration Tools-----Administration Assistant ...
- Android模拟器出现emulator-5554 disconnected! Cancelling activity launch !的解决办法
关于 emulator-5554 disconnected! Cancelling 'xxx activity launch'!的问题,解决方法: d: cd D:/Program Files/and ...
- 编程中&&和||的妙用
&&符号在编程中表示“和”,也就是数学中的“且”! if(A && B){ } 上面的代表表示A==true并且B==true的情况下就执行花括号里面的代码. 值得注意 ...