Definition and assignment of variables in mysql stored procedures _ MySQL

Source: Internet
Author: User
BitsCN.com I. definition of variables
Mysql uses declare to define a local variable. the scope of use of this variable can only be in begin... used in the end block, the variable must be defined at the beginning of the composite statement, and can declare multiple variables at the same time before other statements. if necessary, you can use default to assign the default value.
The syntax for defining a variable is as follows:
Declare var_name [,...] type [default value] to view a variable definition instance
Declare last date; 2. mysql stored procedure variable assignment
You can directly assign values to variables or assign values to queries. you can use set to directly assign values. it can be a constant or expression.

Set var_name = [, var_name expr]... the method for assigning values to the last variable above is as follows:
Set last = date_sub (current_date (), interval 1 month); The following describes how to assign values to the variables through the query. the results returned by the query must be one row. the specific operations are as follows:
Select col into var_name [,...] table_expr we will assign values to v_pay through the query.
Create function get _ cost (p_custid int, p_eff datetime)
Return decimal (5, 2)
Deterministic
Reads SQL data
Begin
Declare v_pay decimail (5, 2 );
Select ifnull (sum (pay. amount), 0) into vpay from payment where pay. payd <= p_eff and pay. custid = pid
Reutrn v_rent + v_over-v_pay;
End $

Now, this simple tutorial on variable definition assignment in the stored procedure is here. next we will talk about the definition and processing of conditions in the myql stored procedure.

The following is a supplement from other netizens:

You can use a variable in the MySQL stored procedure to save the values in the processing procedure.

Use the DECLARE statement to define variables. the syntax format is as follows:

DECLARE var_name [,...] type [DEFAULT value]

Var_name is the variable name and type is any data type supported by MySQL. the optional [DEFAULT value] specifies the DEFAULT value for the variable. Multiple variables of the same type can be defined at a time. variable names are separated by commas.

Pay attention to the following points when defining and using variables:

◆ The DECLARE statement must be used in DEGIN... The END statement block must appear in the DEGIN... The beginning of the END statement block, that is, before other statements.

◆ The scope of the variables defined by DECLARE is limited to the DEGIN... Other DEGIN within the END block and nested in the block... END block.

◆ Variable names in stored procedures are case-insensitive.

The defined variables are assigned values using the SET statement. The syntax format is as follows:

SET var_name = expr [, var_name = expr]...

Here, var_name is the variable name, expr is the expression of the value or return value, which can be any expression supported by MySQL. You can assign values to multiple variables at a time. multiple "variable name = value" pairs are separated by commas.

For example:

Begin
Declare no varchar (20 );
Declare title varchar (30 );
Set no = '000000', title = 'variables and assignments defined in stored procedures ';
End

Tip: All keywords in the stored procedure are case-insensitive. for example, BEGIN can be written. BitsCN.com

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.