GET [CURRENT | STACKED] DIAGNOSTICS {statement_information_item[, statement_information_item] ...| CONDITION condition_numbercondition_information_item[, condition_information_item] ...}statement_information_item:target = statement_information_item_namecondition_information_item:target = condition_information_item_namestatement_information_item_name: {NUMBER| ROW_COUNT}condition_information_item_name: {CLASS_ORIGIN| SUBCLASS_ORIGIN| RETURNED_SQLSTATE| MESSAGE_TEXT| MYSQL_ERRNO| CONSTRAINT_CATALOG| CONSTRAINT_SCHEMA| CONSTRAINT_NAME| CATALOG_NAME| SCHEMA_NAME| TABLE_NAME| COLUMN_NAME| CURSOR_NAME}condition_number, target:(see following discussion)
mysql> DROP TABLE test.no_such_table;ERROR 1051 (42S02): Unknown table 'test.no_such_table'mysql> GET DIAGNOSTICS CONDITION 1@p1 = RETURNED_SQLSTATE, @p2 = MESSAGE_TEXT;mysql> SELECT @p1, @p2;+-------+------------------------------------+| @p1 | @p2 |+-------+------------------------------------+| 42S02 | Unknown table 'test.no_such_table' |+-------+------------------------------------+
Statement information:row count... other statement information items ...Condition area list:Condition area 1:error code for condition 1error message for condition 1... other condition information items ...Condition area 2:error code for condition 2:error message for condition 2... other condition information items ...Condition area 3:error code for condition 3error message for condition 3... other condition information items ...
GET DIAGNOSTICS @p1 = NUMBER, @p2 = ROW_COUNT;
GET DIAGNOSTICS CONDITION 1@p3 = RETURNED_SQLSTATE, @p4 = MESSAGE_TEXT;
mysql> GET DIAGNOSTICS CONDITION 1@p5 = SCHEMA_NAME, @p6 = TABLE_NAME;mysql> SELECT @p5, @p6;+------+------+| @p5 | @p6 |+------+------+| | |+------+------+
GET DIAGNOSTICS CONDITION 1 @errno = MYSQL_ERRNO;
GET DIAGNOSTICS @cno = NUMBER;GET DIAGNOSTICS CONDITION @cno @errno = MYSQL_ERRNO;
CREATE PROCEDURE do_insert(value INT)BEGIN-- Declare variables to hold diagnostics area informationDECLARE code CHAR(5) DEFAULT '00000';DECLARE msg TEXT;DECLARE nrows INT;DECLARE result TEXT;-- Declare exception handler for failed insertDECLARE CONTINUE HANDLER FOR SQLEXCEPTIONBEGINGET DIAGNOSTICS CONDITION 1code = RETURNED_SQLSTATE, msg = MESSAGE_TEXT;END;-- Perform the insertINSERT INTO t1 (int_col) VALUES(value);-- Check whether the insert was successfulIF code = '00000' THENGET DIAGNOSTICS nrows = ROW_COUNT;SET result = CONCAT('insert succeeded, row count = ',nrows);ELSESET result = CONCAT('insert failed, error = ',code,', message = ',msg);END IF;-- Say what happenedSELECT result;END;
mysql> CALL do_insert(1);+---------------------------------+| result |+---------------------------------+| insert succeeded, row count = 1 |+---------------------------------+mysql> CALL do_insert(NULL);+-------------------------------------------------------------------------+| result |+-------------------------------------------------------------------------+| insert failed, error = 23000, message = Column 'int_col' cannot be null |+-------------------------------------------------------------------------+
DROP TABLE IF EXISTS t1;CREATE TABLE t1 (c1 TEXT NOT NULL);DROP PROCEDURE IF EXISTS p;delimiterCREATE PROCEDURE p ()BEGIN-- Declare variables to hold diagnostics area informationDECLARE errcount INT;DECLARE errno INT;DECLARE msg TEXT;DECLARE EXIT HANDLER FOR SQLEXCEPTIONBEGIN-- Here the current DA is nonempty because no prior statements-- executing within the handler have cleared itGET CURRENT DIAGNOSTICS CONDITION 1errno = MYSQL_ERRNO, msg = MESSAGE_TEXT;SELECT 'current DA before mapped insert' AS op, errno, msg;GET STACKED DIAGNOSTICS CONDITION 1errno = MYSQL_ERRNO, msg = MESSAGE_TEXT;SELECT 'stacked DA before mapped insert' AS op, errno, msg;-- Map attempted NULL insert to empty string insertINSERT INTO t1 (c1) VALUES('');-- Here the current DA should be empty (if the INSERT succeeded),-- so check whether there are conditions before attempting to-- obtain condition informationGET CURRENT DIAGNOSTICS errcount = NUMBER;IF errcount = 0THENSELECT 'mapped insert succeeded, current DA is empty' AS op;ELSEGET CURRENT DIAGNOSTICS CONDITION 1errno = MYSQL_ERRNO, msg = MESSAGE_TEXT;SELECT 'current DA after mapped insert' AS op, errno, msg;END IF ;GET STACKED DIAGNOSTICS CONDITION 1errno = MYSQL_ERRNO, msg = MESSAGE_TEXT;SELECT 'stacked DA after mapped insert' AS op, errno, msg;END;INSERT INTO t1 (c1) VALUES('string 1');INSERT INTO t1 (c1) VALUES(NULL);END;//delimiter ;CALL p();SELECT * FROM t1;
+---------------------------------+-------+----------------------------+| op | errno | msg |+---------------------------------+-------+----------------------------+| current DA before mapped insert | 1048 | Column 'c1' cannot be null |+---------------------------------+-------+----------------------------++---------------------------------+-------+----------------------------+| op | errno | msg |+---------------------------------+-------+----------------------------+| stacked DA before mapped insert | 1048 | Column 'c1' cannot be null |+---------------------------------+-------+----------------------------+
+----------------------------------------------+| op |+----------------------------------------------+| mapped insert succeeded, current DA is empty |+----------------------------------------------++--------------------------------+-------+----------------------------+| op | errno | msg |+--------------------------------+-------+----------------------------+| stacked DA after mapped insert | 1048 | Column 'c1' cannot be null |+--------------------------------+-------+----------------------------+
+----------+| c1 |+----------+| string 1 || |+----------+
CREATE PROCEDURE p ()BEGINDECLARE EXIT HANDLER FOR SQLEXCEPTIONBEGIN-- Declare variables to hold diagnostics area informationDECLARE errcount INT;DECLARE errno INT;DECLARE msg TEXT;GET CURRENT DIAGNOSTICS CONDITION 1errno = MYSQL_ERRNO, msg = MESSAGE_TEXT;SELECT 'current DA before mapped insert' AS op, errno, msg;GET STACKED DIAGNOSTICS CONDITION 1errno = MYSQL_ERRNO, msg = MESSAGE_TEXT;SELECT 'stacked DA before mapped insert' AS op, errno, msg;...
+---------------------------------+-------+------+| op | errno | msg |+---------------------------------+-------+------+| current DA before mapped insert | NULL | NULL |+---------------------------------+-------+------++---------------------------------+-------+----------------------------+| op | errno | msg |+---------------------------------+-------+----------------------------+| stacked DA before mapped insert | 1048 | Column 'c1' cannot be null |+---------------------------------+-------+----------------------------+
文章转载自数据库杂货铺,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




