Understanding of stored procedures in MySQL

Source: Internet
Author: User

What exactly is a stored procedure and why do you need to use a stored procedure?

A stored procedure is simply a collection of one or more MySQL statements saved for later use, which can be treated as a batch file, although they are not limited to batch processing.

There are 3 main benefits of using stored procedures, namely, simple, secure, high-performance, in fact, and the call function is almost, only provide input and output, the internal implementation is completely encapsulated up.

Create a stored procedure that has no input parameters and no output parameters, and the process itself is just a simple SQL statement,

The reason why this is not created in the command-line interaction program is because the interaction program defaults to the Terminator of an SQL statement, so that the creation of the stored procedure will be faulted.

Therefore, you need to use delimiter//To temporarily modify the program's statement delimiter, and after the creation of the stored procedure, you need to use delimiter, and then change back, where//can also be converted to other characters

Stored procedure with output parameters

Here we need to add that the keyword out indicates that the corresponding parameter is used to send a value from the stored procedure, MySQL supports in (passed to the stored procedure), out (outgoing from stored procedure) and inout (incoming and outgoing) parameters of the stored procedure type

The code for the stored procedure is between begin and end, and the SELECT statement is used to retrieve the value and then save it to the appropriate variable (by specifying the INTO keyword)

All MySQL variables must start with @k

Note that when MySQL is processing a statement, it ignores whitespace, which is written for easy-to-read SQL statements, and it is also recommended to write

Stored procedures with both input parameters and output parameters

In general, stored procedures are more complex to write than basic SQL statements, and writing stored procedures requires a higher level of skill and richer experience.

Please refer to the "MySQL must Know" book for examples of relevant smart storage.

The above examples refer to the book "MySQL Must Know".

Understanding of stored procedures in MySQL

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.