zoukankan      html  css  js  c++  java
  • Postgresql Errors and Messages

    38.8. Errors and Messages

    Use the RAISE statement to report messages and raise errors.

    from:http://www.postgresql.org/docs/8.4/interactive/plpgsql-errors-and-messages.html

    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. See Chapter 18 for more information.

    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 %.

    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. The allowed option keywords are MESSAGE, DETAIL, HINT, and ERRCODE, while each expression can be any string-valued expression. 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, while HINT supplies a hint message. ERRCODE specifies the error code (SQLSTATE) to report, either by condition name as shown in Appendix A, or directly as a five-character SQLSTATE code.

    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 to the next enclosing block.

    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.

  • 相关阅读:
    java.lang.IllegalArgumentException: Not an managed type
    时间 日期 转换
    纯JavaScript开发飞机大战项目
    【thinkPHP框架】Failed opening required 'header.php' include_path='.;c:php5pear 终级解决方案
    【精编重制版】JavaWeb 入门级项目实战 -- 文章发布系统 (第二节)
    【PHP快速入门】 第二节 php基本语法
    【精编重制版】JavaWeb 入门级项目实战 -- 文章发布系统 (第一节)
    个人博客恢复访问
    【H5 音乐播放实例】第一节 音乐详情页制作(1)
    【php增删改查实例】第二十六节
  • 原文地址:https://www.cnblogs.com/luoyaoquan/p/2638155.html
Copyright © 2011-2022 走看看