Mysql (13) usage of variables and conditions

Source: Internet
Author: User


Links to Mysql variables and conditions: Mysql Installation http://www.bkjia.com/database/201210/162314.html Mysql (2) Database Operations http://www.bkjia.com/database/201210/162315.html Mysql (3) operations on Data Tables http://www.bkjia.com/database/201210/162316.html Mysql (4) Data Table query operations http://www.bkjia.com/database/201210/162317.html Mysql (5) operation time http://www.bkjia.com/database/201210/162318.html;Mysql Those things (6) String Pattern Matching http://www.bkjia.com/database/201210/163969.html;Mysql Those things (7) In-depth select query http://www.bkjia.com/database/201210/163970.html;Mysql Those things (8) Index http://www.bkjia.com/database/201210/163971.html Mysql (9) common functions http://www.bkjia.com/database/201210/164229.html Mysql (10) trigger 1 http://www.bkjia.com/database/201210/164516.html Mysql (11) trigger 2 http://www.bkjia.com/database/201210/164766.html Mysql stored procedures http://www.bkjia.com/database/201210/164795.html In the stored procedure, you can use variables and conditions. 1. Define the variable www.2cto.com. You can use DECLARE to define a variable, but it is local. It can only be effective in BEGIN... END and can be used in nested blocks. Of course, you can use default to set the default value. SQL code DECLARE var_name [,...] type [DEFAULT value] example: Define a variable SQL code DECLARE last_day_start DATE -- Define a DATE type variable named last_day_start 2. you can directly assign values to variable values and query values. SQL code -- directly assign values using set, you can assign constants or assign expressions -- syntax format SET var_name = expr [, var_name = expr ......] example: assign an SQL code SET last_day_start = DATE_SUB (CURRENT_DATE (), INTERVAL 1 MONTH) to the variable defined above) -- assign values to variables: assign values to variables through queries SQL code -- assign values to variables through queries -- syntax format SELECT col_name [,...] INTO var_name [,...] table_expr SQL code -- the example assigns the query result to the variable www.2cto.com SQL code DECLARE v_payment DECIMAL (5, 2); -- defines a variable -- assign the SELECT IFNULL (SUM (payment. amount), 0) INTO v_pay Ment FROM payment 3. SQL code FOR CONDITION definition -- syntax format of CONDITION definition: DECLARE condition_name CONDITION FOR condition_value: SQLSTATE [VALUE] sqlstate_value | mysql_error_code 4. SQL code FOR processing conditions --- DECLARE handler_type HANDLER FOR condition_value [,...] sp_statement handler_type: CONTINUE | EXIT | UNDO condition_value: SQLSTATE [VALUE] | condition_name | SQLWARNING | not found | SQLEXCEPTION | mysql_error_co De example: the SQL code DECLARE CONTINUE HANDLER FOR SQLSTATE '000000' SET @ x2 = 1 CONTINUE indicates continued downward execution; EXIT indicates execution aborted.
 

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.