?
This document uses PHP Chinese website manual Release
利用RAISE語句報告信息以及拋出錯誤。
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 ;
level選項聲明了錯誤的嚴重性等級。 可能的級別有DEBUG(向服務器日志寫信息), LOG(向服務器日志寫信息,優(yōu)先級更高), INFO, NOTICE, WARNING(把信息寫到服務器日志以及轉(zhuǎn)發(fā)到客戶端應用, 優(yōu)先級逐步升高), EXCEPTION(拋出一個錯誤,通常退出當前事務),默認的是EXCEPTION。 EXCEPTION會拋出一個錯誤(強制關閉當天事務),而其他級別僅僅是產(chǎn)生不同的優(yōu)先級信息。 無論是將優(yōu)先級別的信息是報告給客戶端,還是寫到服務器日志,亦或是二者都是, 都是由log_min_messages和client_min_messages配置變量控制的。 參閱Chapter 18獲取更多細節(jié)
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 %. 在格式字符串里,%被下一個可選參數(shù)的外部表現(xiàn)形式代替。 要表示%字符必須雙寫(%%)。 參數(shù)可以是簡單的變量或者表達式,而格式必須是一個簡單的字符串文本。
在這個例子里,v_job_id的值將代替字符串中的%:
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.
該例子會強制退出事務,并返回如下提示:
RAISE EXCEPTION 'Nonexistent ID --> %', user_id USING HINT = 'Please check your user id';
下面兩個例子在設置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';
在這個語法中,USING可以來提供一個通用的錯誤信息,另一個例子是:
RAISE unique_violation USING MESSAGE = 'Duplicate user ID: ' || user_id;
另一個變形是寫RAISE USING或RAISE level USING, 然后將其他的所有東西都放在USING列中。
最后一個RAISE變形中沒有任何參數(shù)。這種形式只能在BEGIN塊的EXCEPTION字句中使用。 它的作用是將正在處理的錯誤放到下一個封閉的塊中。
如果RAISE EXCEPTION中沒有聲明SQLSTATE的情形名稱,那么缺省使用RAISE_EXCEPTION (P0001)。 如果沒有聲明信息文本,那么缺省將情形名稱或SQLSTATE作為信息文本。
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.