Mysql Stored Procedure

Source: Internet
Author: User

The MySQL stored procedure is a new feature added since MySQL 5.0.

The advantages of stored procedures are as follows. However, the most important thing is the execution efficiency and SQL code encapsulation.

In particular, the SQL code encapsulation function. If there is no stored procedure, many SQL statements need to be organized when external programs access the database (such as PHP. Especially when the business logic is complex, a lot of SQL statements and conditions are mixed in PHP code, which is chilling. With the MySQL stored procedure, the business logic can encapsulate the stored procedure, which is not only easy to maintain, but also highly efficient in execution.

I. MySQL Stored Procedure
"Pr_add" is a simple MySQL stored procedure, which has two int-type input parameters: "a" and "B", and returns the sum of the two parameters.

        c  a          a    b          b    c  a  c  ;

 

Ii. Call the MySQL Stored Procedure

call pr_add(, );


Execute the MySQL stored procedure. The stored procedure parameter is a MySQL user variable.

      , );

Iii. MySQL stored procedure features
The simple syntax for creating a MySQL stored procedure is:

 ;

1. The "()" after the MySQL stored procedure name is required. Even if there is no parameter, "()" is required.
2. For MySQL stored procedure parameters, you cannot add "@" before the parameter name, for example, int ". The following syntax for creating a stored procedure is incorrect in MySQL (correct in SQL Server ). You do not need to add "@" before the variable name in the MySQL stored procedure, although the MySQL client user variable requires "@".

  ,     b     )

4. You do not need to add "as" before procedure body to the MySQL stored procedure ". The SQL Server Stored Procedure must contain the "as" keyword.

. If the MySQL Stored Procedure contains multiple MySQL statements, you need;

 c  a          a   

     c ;         a    a   ;

  c  a  c  ;

call pr_no_param();

call pr_add(, );

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.