MySQL stored procedure creation Solution

Source: Internet
Author: User
The following article describes the actual operation analysis of the MySQL stored procedure. We all know that a MySQL stored procedure mainly includes the name and parameter list, there are also a lot of SQL statements and SQL statement sets. The following describes how to create a MySQL stored procedure: Syntax: CREATEPROCEDUREp () BEGIN * Body of the stored procedure *

The following article describes the actual operation analysis of the MySQL stored procedure. We all know that a MySQL stored procedure mainly includes the name and parameter list, there are also a lot of SQL statements and SQL statement sets. The following describes how to create a MySQL stored procedure: Syntax: CREATEPROCEDUREp () BEGIN/* Body of the stored procedure *

The following article describes the actual operation analysis of the MySQL stored procedure. We all know that a MySQL stored procedure mainly includes the name and parameter list, there are also a lot of SQL statements and SQL statement sets. The following describes the specific content,

Create a MySQL stored procedure:

Syntax:

 
 
  1. CREATE PROCEDURE p()
  2. BEGIN

/* Body of the stored procedure */

 
 
  1. END
  2. CREATE PROCEDURE productpricing()
  3. BEGIN
  4. SELECT Avg(pro_price) AS priceaverage
  5. FROM products;
  6. 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 MySQL stored procedure defined above

 
 
  1. 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:

 
 
  1. DROP PROCUDURE productpricing;

Create a stored procedure with parameters:

 
 
  1. CREATE PROCUDURE productpricing(
  2. OUT p1 DECIMAL(8,2),
  3. OUT ph DECIMAL(8,2),
  4. OUT pa DECIMAL(8,2)
  5. )
  6. BEGIN
  7. SELECT Min(prod_price) INTO pl FROM products;
  8. SELECT Max(prod_price) INTO ph FROM products;
  9. SELECT Avg(prod_price) INTO pa FROM products;
  10. END;

DECIMAL is used to specify the Data Type of a parameter.

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

MySQL supports OUT, IN, and INOUT

Call the MySQL stored procedure with parameters:

 
 
  1. CALL productpricing(@pricelow,
  2. @pricehigh,
  3. @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:

 
 
  1. SELECT @pricehigh, @pricelow, @priceaverage;

Another stored procedure with IN and OUT parameters:

 
 
  1. CREATE PROCEDURE ordertotal(
  2. IN onumber INT,
  3. OUT ototal DECIMAL(8,2)
  4. )
  5. BEGIN
  6. SELECT Sum(item_price*quantity)
  7. FROM orderitems
  8. WHERE order_num = onumber
  9. INTO ototal;
  10. END;
  11. CALL ordertotal(20005, @total);
  12. SELECT @total;

Add a complete example: (this is a custom paging MySQL Stored Procedure)

 
 
  1. DELIMITER $$
  2. DROP PROCEDURE IF EXISTS `dbcall`.`get_page`$$
  3. CREATE DEFINER=`root`@`localhost` PROCEDURE `get_page`(
  4. /**//*Table name*/
  5. tableName varchar(100),
  6. /**//*Fileds to display*/
  7. fieldsNames varchar(100),
  8. /**//*Page index*/
  9. pageIndex int,
  10. /**//*Page Size*/
  11. pageSize int,
  12. /**//*Field to sort*/
  13. sortName varchar(500),
  14. /**//*Condition*/
  15. strWhere varchar(500)
  16. )
  17. BEGIN
  18. DECLARE fieldlist varchar(200);
  19. if fieldsNames=''||fieldsNames=null THEN
  20. set fieldlist='*';
  21. else
  22. set fieldlist=fieldsNames;
  23. end if;
  24. if strWhere=''||strWhere=null then
  25. if sortName=''||sortName=null then
  26. set @strSQL=concat('SELECT ',fieldlist,' FROM ',tableName,' LIMIT ',(pageIndex-1)*pageSize,',',pageSize);
  27. else
  28. set @strSQL=concat('SELECT ',fieldlist,' FROM ',tableName,' ORDER BY ',sortName,' LIMIT ',(pageIndex-1)*pageSize,',',pageSize);
  29. end if;
  30. else
  31. if sortName=''||sortName=null then
  32. set @strSQL=concat('SELECT ',fieldlist,' FROM ',tableName,' WHERE ',strWhere,' LIMIT ',(pageIndex-1)*pageSize,',',pageSize);
  33. else
  34. set @strSQL=concat('SELECT ',fieldlist,' FROM ',tableName,' WHERE ',strWhere,' ORDER BY ',sortName,' LIMIT ',(pageIndex-1)*pageSize,',',pageSize);
  35. end if;
  36. end if;
  37. PREPARE stmt1 FROM @strSQL;
  38. EXECUTE stmt1;
  39. DEALLOCATE PREPARE stmt1;
  40. END$$
  41. DELIMITER ;

The above content is an introduction to the MySQL stored procedure. I hope you will get some benefits.

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.