Definition and assignment operation of variables in MySQL stored procedure _mysql

Source: Internet
Author: User
Definition of a variable
The variable definition in MySQL uses declare to define a local variable, which can only be used in a begin...end block, and the variable must be defined at the beginning of the compound statement, and before other statements, and can also declare multiple variables, if necessary, You can assign defaults using default values.
Define a variable syntax as follows:
DECLARE var_name[,...] type[default value] Look at a variable definition instance
Declare last date second, MySQL stored procedure variable assignment
The assignment of a variable can be directly assigned to a query assignment, and direct assignment can be done with set, or it can be a constant or an expression.
Copy Code code as follows:

Set var_name= [, Var_name expr] ... Assign the last variable to the above method as follows
Set last = Date_sub (Current_date (), interval 1 month); The following is a query to assign a value to a variable, asking the query to return the result must be a row, the following actions
Select col into var_name[,...] table_expr us to assign values to V_pay through queries.
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 the Vpay from payment where Pay.payd<=p_eff and Pay.custid=pid
REUTRN v_rent + v_over-v_pay;
End $$

OK, this is a simple procedure for defining variables in the stored procedures. The tutorial is here, and we'll go on to define and process the conditions for MYQL stored procedures.

The following are the supplements of other netizens

In the MySQL stored procedure, you can use variables, which are used to hold values during processing.

Define variables using the Declare statement, the syntax format is as follows:

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

Where Var_name is the variable name, the type is any data type supported by MySQL, and the option [default value] Specifies the default value for the variable. You can define multiple variables of the same type at one time, separated by commas "" between the variable names.

The following points need to be noted when defining and using variables:

Declare statement must be used in degin ... End statement block, and must appear in the Degin ... The front of the end statement block, which appears before other statements.

The scope of the variable defined by declare is limited to the degin of the Declare statement ... End block and other degin nested within the block ... End block.

Variable names in stored procedures are case-insensitive.

The defined variables are assigned using the SET statement, and the syntax format is as follows:

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

Where Var_name is the variable name, expr is a value, or an expression that returns a value, so that any mysql-supported expression of the return value. You can assign values to more than one variable at a time, and separate multiple variable name = value pairs with commas.

For example:
Copy Code code as follows:

Begin
DECLARE no varchar (20);
DECLARE title varchar (30);
Set no= ' 101010 ', title= ' stored procedures define variables and assignments ';
End

Tip: All keywords in a stored procedure are case-insensitive, such as begin to write a begin.
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.