Definition assignment of variables in mysql stored procedures

Source: Internet
Author: User

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.

Yesterday we talked about how to create, modify, and delete the stored procedure in the mysql tutorial. The following is a tutorial aboutDefinition assignment of variables in mysql stored proceduresOh.

I,Variable definition

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]

View a variable definition instance

Declare last date;

II,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 above last variable is as follows:

Set last = date_sub (current_date (), interval 1 month );

The following describes how to assign a value to a variable through a query. The result returned by the query must be a row. The specific operation is as follows:

Select col into var_name [,...] table_expr

Query to assign values to v_pay.

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 aboutMyql Stored Procedure.

Indicate, this site Original article reprinted indicate the source http://www.bKjia. c0m/database/database.html

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.