Home >Database >Mysql Tutorial >MySQL 存储过程的创建方案
以下的文章主要介绍的是MySQL 存储过程的实际操作解析,我们大家都知道一个MySQL 存储过程主要包括名字,参数列表,还有包括很多的SQL语句与SQL语句集。以下就是具体内容的描述, 创建MySQL 存储过程: 语法: CREATEPROCEDUREp() BEGIN /*此存储过程的正文*
以下的文章主要介绍的是MySQL 存储过程的实际操作解析,我们大家都知道一个MySQL 存储过程主要包括名字,参数列表,还有包括很多的SQL语句与SQL语句集。以下就是具体内容的描述,
创建MySQL 存储过程:
语法:
<ol class="dp-xml"> <li class="alt"><span><span>CREATE PROCEDURE p() </span></span></li> <li><span>BEGIN </span></li> </ol>
/*此存储过程的正文*/
<ol class="dp-xml"> <li class="alt"><span><span>END </span></span></li> <li><span>CREATE PROCEDURE productpricing() </span></li> <li class="alt"><span>BEGIN </span></li> <li><span>SELECT Avg(pro_price) AS priceaverage </span></li> <li class="alt"><span>FROM products; </span></li> <li><span>END; </span></li> <li class="alt"><span> </span></li> </ol>
begin…end之间是存储过程的主体定义
MySQL的分界符是分号(;)
调用存储过程的方法是:
CALL加上过程名以及一个括号
例如调用上面定义的MySQL 存储过程
<ol class="dp-xml"><li class="alt"><span><span>CALL productpricing(); </span></span></li></ol>
哪怕是不用传递参数,存储过程名字后面的括号“()”也是必须的
删除存储过程的方法是:
<ol class="dp-xml"><li class="alt"><span><span>DROP PROCUDURE productpricing; </span></span></li></ol>
创建带参数的存储过程:
<ol class="dp-xml"> <li class="alt"><span><span>CREATE PROCUDURE productpricing( </span></span></li> <li><span>OUT p1 DECIMAL(8,2), </span></li> <li class="alt"><span>OUT ph DECIMAL(8,2), </span></li> <li><span>OUT pa DECIMAL(8,2) </span></li> <li class="alt"><span>) </span></li> <li><span>BEGIN </span></li> <li class="alt"><span>SELECT Min(prod_price) INTO pl FROM products; </span></li> <li><span>SELECT Max(prod_price) INTO ph FROM products; </span></li> <li class="alt"><span>SELECT Avg(prod_price) INTO pa FROM products; </span></li> <li><span>END; </span></li> <li class="alt"><span> </span></li> </ol>
DECIMAL用于指定参数的数据类型
OUT用于表明此值是用于从存储过程里输出的
MySQL支持 OUT, IN, INOUT
调用带参数的MySQL 存储过程:
<ol class="dp-xml"> <li class="alt"><span><span>CALL productpricing(@pricelow, </span></span></li> <li><span>@pricehigh, </span></li> <li class="alt"><span>@priceaverage); </span></li> </ol>
所有的参数必须以@开头
要想获取@priceaverage的值,用以下语句
SELECT @priceaverage;
获取三个的值,用以下语句
<ol class="dp-xml"><li class="alt"><span><span>SELECT @pricehigh, @pricelow, @priceaverage; </span></span></li></ol>
另一个带IN和OUT参数的存储过程:
<ol class="dp-xml"> <li class="alt"><span><span>CREATE PROCEDURE ordertotal( </span></span></li> <li><span>IN onumber INT, </span></li> <li class="alt"><span>OUT ototal DECIMAL(8,2) </span></li> <li><span>) </span></li> <li class="alt"><span>BEGIN </span></li> <li><span>SELECT Sum(item_price*quantity) </span></li> <li class="alt"><span>FROM orderitems </span></li> <li> <span>WHERE </span><span class="attribute">order_num</span><span> = </span><span class="attribute-value">onumber</span><span> </span> </li> <li class="alt"><span>INTO ototal; </span></li> <li><span>END; </span></li> <li class="alt"><span>CALL ordertotal(20005, @total); </span></li> <li><span>SELECT @total; </span></li> </ol>
添加一个完整的例子:(这是一个自定义分页的MySQL 存储过程)
<ol class="dp-xml"> <li class="alt"><span><span>DELIMITER $$ </span></span></li> <li><span>DROP PROCEDURE IF EXISTS `dbcall`.`get_page`$$ </span></li> <li class="alt"> <span>CREATE </span><span class="attribute">DEFINER</span><span>=`root`@`localhost` PROCEDURE `get_page`( </span> </li> <li><span>/**//*Table name*/ </span></li> <li class="alt"><span>tableName varchar(100), </span></li> <li><span>/**//*Fileds to display*/ </span></li> <li class="alt"><span>fieldsNames varchar(100), </span></li> <li><span>/**//*Page index*/ </span></li> <li class="alt"><span>pageIndex int, </span></li> <li><span>/**//*Page Size*/ </span></li> <li class="alt"><span>pageSize int, </span></li> <li><span>/**//*Field to sort*/ </span></li> <li class="alt"><span>sortName varchar(500), </span></li> <li><span>/**//*Condition*/ </span></li> <li class="alt"><span>strWhere varchar(500) </span></li> <li><span>) </span></li> <li class="alt"><span>BEGIN </span></li> <li><span>DECLARE fieldlist varchar(200); </span></li> <li class="alt"> <span>if </span><span class="attribute">fieldsNames</span><span>=</span><span class="attribute-value">''</span><span>||</span><span class="attribute">fieldsNames</span><span>=</span><span class="attribute-value">null</span><span> THEN </span> </li> <li> <span>set </span><span class="attribute">fieldlist</span><span>=</span><span class="attribute-value">'*'</span><span>; </span> </li> <li class="alt"><span>else </span></li> <li> <span>set </span><span class="attribute">fieldlist</span><span>=</span><span class="attribute-value">fieldsNames</span><span>; </span> </li> <li class="alt"><span>end if; </span></li> <li> <span>if </span><span class="attribute">strWhere</span><span>=</span><span class="attribute-value">''</span><span>||</span><span class="attribute">strWhere</span><span>=</span><span class="attribute-value">null</span><span> then </span> </li> <li class="alt"> <span>if </span><span class="attribute">sortName</span><span>=</span><span class="attribute-value">''</span><span>||</span><span class="attribute">sortName</span><span>=</span><span class="attribute-value">null</span><span> then </span> </li> <li> <span>set @</span><span class="attribute">strSQL</span><span>=</span><span class="attribute-value">concat</span><span>('SELECT ',fieldlist,' FROM ',tableName,' LIMIT ',(pageIndex-1)*pageSize,',',pageSize); </span> </li> <li class="alt"><span>else </span></li> <li> <span>set @</span><span class="attribute">strSQL</span><span>=</span><span class="attribute-value">concat</span><span>('SELECT ',fieldlist,' FROM ',tableName,' ORDER BY ',sortName,' LIMIT ',(pageIndex-1)*pageSize,',',pageSize); </span> </li> <li class="alt"><span>end if; </span></li> <li><span>else </span></li> <li class="alt"> <span>if </span><span class="attribute">sortName</span><span>=</span><span class="attribute-value">''</span><span>||</span><span class="attribute">sortName</span><span>=</span><span class="attribute-value">null</span><span> then </span> </li> <li> <span>set @</span><span class="attribute">strSQL</span><span>=</span><span class="attribute-value">concat</span><span>('SELECT ',fieldlist,' FROM ',tableName,' WHERE ',strWhere,' LIMIT ',(pageIndex-1)*pageSize,',',pageSize); </span> </li> <li class="alt"><span>else </span></li> <li> <span>set @</span><span class="attribute">strSQL</span><span>=</span><span class="attribute-value">concat</span><span>('SELECT ',fieldlist,' FROM ',tableName,' WHERE ',strWhere,' ORDER BY ',sortName,' LIMIT ',(pageIndex-1)*pageSize,',',pageSize); </span> </li> <li class="alt"><span>end if; </span></li> <li><span>end if; </span></li> <li class="alt"><span>PREPARE stmt1 FROM @strSQL; </span></li> <li><span>EXECUTE stmt1; </span></li> <li class="alt"><span>DEALLOCATE PREPARE stmt1; </span></li> <li><span>END$$ </span></li> <li class="alt"><span>DELIMITER ; </span></li> </ol>
以上的相关内容就是对MySQL 存储过程的介绍,望你能有所收获。