MySQL 存储过程是从 MySQL 5.0 开始增加的新功能。
存储过程最主要优点是执行效率和SQL 代码封装。
相对于SQL Server和Oracle存储过程写法大体类似,不过有些特殊语法要注意。
最简洁的语法结构如下:
DELIMITER $$
CREATE
PROCEDURE `dbbook`.`test`() //在dbbook数据中创建名为test的存储过程
BEGIN
END$$
DELIMITER ;
修改存储过程:
DELIMITER $$
USE `benq_ebook`$$
DROP PROCEDURE IF EXISTS `user_buy_analyze`$$
CREATE DEFINER=`root`@`%` PROCEDURE `user_buy_analyze`(IN antype VARCHAR (10),IN datefrom VARCHAR (10),IN dateto VARCHAR (50))
BEGIN
DECLARE sumuser INT ;
IF antype!='rec'
THEN
SET datefrom= CONCAT(datefrom,' 00:00:00');
SET dateto= CONCAT(dateto,' 23:59:59');
SELECT
COUNT(*) INTO sumuser
FROM
user_profile AS u
WHERE u.uid IN
(SELECT
lo.uid
FROM
log_order AS lo
INNER JOIN log_order_book AS lob
ON lo.orderNo = lob.orderNo
AND lob.goods_type != '08'
AND lob.b_pay_status=1
AND lo.o_est_datetime BETWEEN datefrom AND dateto);
END IF;
CASE
antype -- 性别百分比
WHEN 'sex'
THEN
BEGIN
SELECT
CASE u.sex
WHEN 'F'
THEN N'女'
WHEN 'M'
THEN