Raising Error Conditions with MySQL SIGNAL/RESIGNAL Statements, raisingresignal

Source: Internet
Author: User

Raising Error Conditions with MySQL SIGNAL/RESIGNAL Statements, raisingresignal
Http://www.mysqltutorial.org/mysql-signal-resignal/

Summary: In this tutorial, you will learn how to useSIGNALAndRESIGNALStatements to raise error conditions inside stored procedures.

MySQL SIGNAL statement

You useSIGNALStatement to return an error or warning condition to the caller from a stored program e.g., stored procedure, stored function, trigger or event.SIGNALStatement provides you with control over which information for returning such as value and messageSQLSTATE.

The following extends strates syntax ofSIGNALStatement:

 
123 Signal sqlstate | condition_name; SET condition_information_item_name_1 = value_1, condition_information_item_name_1 = value_2, etc;

FollowingSIGNALKeyword isSQLSTATEValue or a condition name declared byDECLARE CONDITIONStatement. Notice thatSIGNALStatement must always specifySQLSTATEValue or a named condition that defined withSQLSTATEValue.

To provide the caller with information, you useSETClause. If you want to return multiple condition information item names with values, you need to separate each name/value pair by a comma.

Thecondition_information_item_nameCan beMESSAGE_TEXT,MYSQL_ERRORNO,CURSOR_NAME, Etc.

The following stored procedure adds an order line item into an existing sales order. It issues an error message if the order number does not exist.

 
1234567891011121314151617181920212223 DELIMITER $ create procedure AddOrderItem (in orderNo int, in productCode varchar (45), in qty int, in price double, in lineNo int) begin declare c int; select count (orderNumber) into c from orders WHERE orderNumber = orderNo; -- check if orderNumber exists IF (C! = 1) then signal sqlstate '200' SET MESSAGE_TEXT = 'order No not found in orders table'; end if; -- more code below --... END

First, it counts the orders with the input order number that we pass to the stored procedure.

Second, if the number of order is not 1, it raises an errorSQLSTATE 45000Along with an error message saying that order number does not exist in the orders table.

Notice that45000Is a genericSQLSTATEValue that has strates an unhandled user-defined exception.

If we call the stored procedureAddOrderItem()And pass a nonexistent order number, we will get an error message.

 
1 CALL AddOrderItem (10, 's10 _ 1678 ', 1, 95.7, 1 );

MySQL RESIGNAL statement

BesidesSIGNALStatement, MySQL also providesRESIGNALStatement used to raise a warning or error condition.

TheRESIGNALStatement is similarSIGNALStatement in term of functionality and syntax, doesn t that:

  • You must useRESIGNALStatement within an error or warning handler, otherwise, you will get an error message saying that "RESIGNAL when handler is not active". Notice that you can useSIGNALStatement anywhere inside a stored procedure.
  • You can omit all attributes ofRESIGNALStatement, evenSQLSTATEValue.

If you useRESIGNALStatement alone, all attributes are the same as the ones passed to the condition handler.

The following stored procedure changes the error message before issuing it to the caller.

 
123456789101112131415 DELIMITER $ create procedure Divide (IN numerator INT, IN denominator INT, OUT result double) begin declare division_by_zero condition for sqlstate '200 '; declare continue handler for processing resignal set MESSAGE_TEXT = 'division by zero/Denominator cannot be 0'; -- IF denominator = 0 then signal division_by_zero; else set result: = numerator/denominator; end if; END

Let's callDivide()Stored procedure.

 
1 CALL Divide (10, 0, @ result );

In this tutorial, we have shown you how to raise error conditions inside stored programs usingSIGNALAndRESIGNALStatements.

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.