Error Handler ExamplesHere are some examples of handler declarations:If any error condition arises (other than a not FOUND), continue execution after setting L_error=1:DECLARE CONTINUE HANDLER for SQLEXCEPTIONSET l_error=1;If any error condition arises ("other than a not FOUND"), exit the current block or stored program after issuing a ROLLBACK Statement and issuing an error message:DECLARE EXIT HANDLER for SQLEXCEPTIONBEGINROLLBACK;SELECT ' Error occurred–terminating ';End;If MySQL Error 1062 (
Problems encountered in the project
I. Spring transaction problems
1. Description: a in service1 calls B, B calls c in service2, and c calls d in service3
Expectation: When d throws an exception (the SQL exception is thrown in my real project), d and c will be rolled back, while a and B will not be rolled back.
Test: Considering the self-tuning of Spring transactions and the spring transaction configuration under the cglib dynamic proxy. Added
Demo:
Custom exception:Public class MyException ext
, @ pcount );
This stored procedure is to grant the value to the variable by select;
The table does not have the depid = 101 and name = 'Chen 'record.
III. conditions
The function of a condition is generally used to process a specified condition. for example, what should we do if we encounter a primary key repeat error.
Define conditions
Defining a condition is to define the name of an error state or SQL state in advance, and then you can reference the condition name for conditional processin
;SQL1035NThedatabaseiscurrentlyinuse.SQLSTATE=57019
=>db2backupdatabasedbnameto/xx/xxcompress
> SQL1035N The database is currently in use. SQLSTATE = 57019 solution:
SQL code
=>db2stopforce
=>db2start
=>db2stopforce
=>db2start
5. When creating a tablespace, the system reports the error 54047. For example:
SQL code
=>createregulartablespacespace1pagesize4kmanagedbydatabaseusing(device'/dev/dd'25000000)prefetchsizeautoma
Troubleshooting of exceptions in mysql stored procedures, mysql stored procedures
Define the exception capture type and handling method:
DECLARE handler_action HANDLER FOR condition_value [, condition_value] ... statement handler_action: CONTINUE | EXIT | UNDO condition_value: mysql_error_code | SQLSTATE [VALUE] sqlstate_value | condition_name | SQLWARNING | NOT FOUND | SQLEXCEPTION
Pay attention to t
Define the type of exception capture and how to handle it:
DECLARE handler_action Handler
for Condition_value [, Condition_value] ...
Statement
handler_action:
CONTINUE
| EXIT
| UNDO
condition_value:
mysql_error_code
| SQLSTATE [VALUE] sqlstate_value
| condition_name
| SQLWarning
| Not FOUND
| SQLEXCEPTION
There are a few things to note here:
A, Condition_value [, Condit
--The article content through the network search combination,MySQL exception, you can customize the exception, and then apply. You can also use the system default exception to capture the app.First, the exception definition:DECLARE condition_name condition for [condition_type];The condition_name parameter represents the name of the exception;condition_type Parameter exception typeCondition_type consists of SQLState [VALUE] Sqlstate_value|mysql_error_c
. Java is run twice, the following error message is returned:
SQLException: There is already an object named 'COFFEES'in the database.Severity 16, State 1, Line 1
This example shows how to output the Message parts of the sqlexception object. In most cases, this is enough to indicate an error.
However, this object actually has three parts. To fully understand the cause of the error, You can output all of them. The following code snippet shows the catch blocks completed in two ways. T
variable;
There is no record of depid=101 and Name= ' Chen ' in the table.
Third, the conditions
The role of the condition is generally used in the processing of the specified conditions, such as we encounter the primary key repeated error after how to deal with.
Define criteria
Defining a condition is to define a certain error state or the name of the SQL state beforehand, and then you can refer to the condition name to do the conditional processing, the definition condition generally u
automatically close after the declared Begin...end statement block executes;
The cursor declare is used to declare a data collection that corresponds to a cursor and a specified cursor, typically a data collection that is a SELECT statement. DECLARE cursor_name CURSOR FOR select_statement Cursor fetch is used to get the next row of data for the specified data set of the cursor and assign each field value to the subsequent variable FETCH [[NEXT] FROM] cursor_name INTO var_name [, var_name]
Define the type of exception capture and how to handle it: DECLARE handler_action Handler for Condition_value [, Condition_value] ... Statement handler_action: CONTINUE | EXIT | UNDO condition_value: mysql_error_code | SQLSTATE [VALUE] sqlstate_value | condition_name | SQLWarning | Not FOUND | SQLEXCEPTION Here are a few things to note:A, Con
PC
Update brandproductkey as bpk,
(Select bp. brandproductid, bp. brandproductenname, bp. brandid
From brandproduct as bp
Inner join (select brandid, brandproductid from brandproductdriverrelation group by brandid, brandproductid) as bpr
On bp. brandid = bpr. brandid and bp. brandproductid = bpr. brandproductid and bp. brandid = 36
) As bpp
Set bpk. brandproductid = bpp. brandproductid
Where bpk. brandproductid = 0
-- And bpk. computertype = 2 -- 0
And bpk. brandid = bpp. brandid
And bpk. brand
above is our program. The first statement declare exit handler is used to handle exceptions. It means that if error 1215 occurs, this program inserts a row in the error record table. EXIT means to EXIT the compound statement after the action is successfully submitted.5. Sample Problem: Log Of ErrorsCALL p22 (5 )//Calling this stored procedure will fail, which is normal because the value 5 does not appear in the primary key table. However, no error message is returned because the error processin
handler_type handler for condition_value [,...] sp_statement handler_type: CONTINUE | EXIT condition_value: SQLSTATE [VALUE] sqlstate_value | condition_name | SQLWARNING | not found | SQLEXCEPTION | mysql_error_code Handlers type: 1, EXIT: exit the current code block when an error occurs (may be a subcode block or main code block) 2. CONTINUE: CONTINUE to execute the subsequent code condition_value: condition_value when an error is sent. standard
. brandproductenname) = upper (concat (bpk. brandproductkeyname, 'notebook pc '));Insert into t_runninglog values (default, default, 'rule-hp 2 in mergebrandproductkey ','');Commit;--*/-- Task execution subject endsInsert into t_runninglog values (default, default, 'finish in mergebrandproductkey ','');Commit;END
The syntax structure of HANDLER is as follows:Copy codeThe Code is as follows: DECLARE handler_type handler for condition_value [,...] sp_statementHandler_type: CONTINUE | EXITCondition
How to declare a stored procedureCREATE PROCEDURE Stored procedure name (in input variable name input variable type, out output variable name output variable type)followed by a list of stored procedure propertiesCommonly used are: LANGUAGE sql, modifies SQL DATA, result sets 1 (number of result sets returned)l The stored procedure body begins with beginL End of stored procedure body with endStored procedure constraint rulesCalling stored procedures in stored proceduresCall stored procedure name
20.2.10. Conditions and handlers20.2.10.1. DECLARE conditions20.2.10.2. DECLARE handlersSpecific conditions require specific processing. These conditions can be linked to errors, as well as general process control in the subroutine.20.2.10.1. DECLARE conditionsCondition_name Condition_valueCondition_value:Sqlstate_valueMysql_error_codeThis statement specifies the conditions that require special handling. It associates a name with the specified error condition. This name can then be used in the d
Use of the DECLARE handler: DECLAREhandler_typeHANDLERFORcondition_value [,...] sp_statement where the VALUE of handler_type is: CONTINUE | EXIT | UNDOcondition_value. VALUE range: SQLSTATE [VALUE] sqlstate_value | condition_name | SQLWARNING | NOT
Use of the declare handler: DECLARE handler_type handler for condition_value [,...] sp_statement where the VALUE of handler_type is: CONTINUE | EXIT | UNDO condition_value. VALUE range:
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.