MySql Stored Procedure

Source: Internet
Author: User
Introduction:A stored procedure includes a name, a list of parameters, and a set of SQL statements that can contain many SQL statements.

A stored procedure includes a name, a list of parameters, and a set of SQL statements that can contain many SQL statements.

Create a stored procedure:

Syntax:

Create procedure p ()

BEGIN

/* Body of the stored procedure */

END

Create procedure productpricing ()

BEGIN

SELECT Avg (pro_price) AS priceaverage

FROM products;

END;

# Begin... End is the subject definition of the stored procedure.

# The mysql Delimiter is a semicolon (;)

The method to call a stored procedure is:

# Add the process name and brackets to the CALL.

# For example, call the stored procedure defined above

CALL productpricing ();

# Even if no parameter is required, the brackets () after the stored procedure name are required.

To delete a stored procedure, follow these steps:

Drop procudure productpricing;

Create a stored procedure with parameters:

Create procudure productpricing (

OUT p1 DECIMAL (8, 2 ),

OUT ph DECIMAL (8, 2 ),

OUT pa DECIMAL (8, 2)

)

BEGIN

SELECT Min (prod_price) INTO pl FROM products;

SELECT Max (prod_price) INTO ph FROM products;

SELECT Avg (prod_price) INTO pa FROM products;

END;

# DECIMAL: Specifies the Data Type of a parameter.

# OUT indicates that this value is output from the stored procedure.

# MySQL supports OUT, IN, and INOUT

Call a stored procedure with parameters:

CALL productpricing (@ pricelow,

@ Pricehigh,

@ Priceaverage );

# All parameters must start @

# To obtain the value of @ priceaverage, use the following statement

SELECT @ priceaverage;

# Use the following statement to obtain three values:

SELECT @ pricehigh, @ pricelow, @ priceaverage;

Another stored procedure with IN and OUT parameters:

Create procedure ordertotal (

IN onumber INT,

OUT ototal DECIMAL (8, 2)

)

BEGIN

SELECT Sum (item_price * quantity)

FROM orderitems

WHERE order_num = onumber

INTO ototal;

END;

CALL ordertotal (20005, @ total );

SELECT @ total;

Add a complete example:

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.