Definition assignment operations for variables in MySQL stored procedures

Source: Internet
Author: User

Yesterday we talked about the MySQL tutorial stored procedure creation modification and deletion, this tutorial is about the definition assignment of variables in MySQL stored procedure Oh.

definition of a variable

Define a variable syntax as follows:

declare var_name[,...] Type[default value]

Look at a variable definition instance

declare last date;

Second, theMySQL 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.

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

We're going to assign a value to V_pay through a 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 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 .

Note that the original site reproduced the source http://www.111cn.net/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.