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 N'男'
ELSE N'其他'
END AS sex,
u.sex AS sexid,
(COUNT(*) / sumuser)*100 AS sexpercent
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)
GROUP BY u.sex
ORDER BY u.sex ;
END ;
-- 婚姻百分比
WHEN 'mar'
THEN BEGIN
SELECT
CASE
u.marriage
WHEN 1
THEN N'未婚'
WHEN 2
THEN N'已婚,無小孩'
WHEN 3
THEN N'已婚,有小孩'
ELSE N'其他'
END AS mar,
IFNULL(u.marriage,0) AS marid,
(COUNT(*) / sumuser)*100 AS marpercent
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)
GROUP BY u.marriage
ORDER BY u.marriage ;
END;
-- 教育
WHEN 'edu'
THEN
BEGIN
SELECT
CASE
u.education
WHEN 1
THEN N'國中'
WHEN 2
THEN N'高中(職)'
WHEN 3
THEN N'大學'
WHEN 4
THEN N'碩士'
WHEN 5
THEN N'博士'
ELSE N'其他'
END AS edu,
IFNULL(u.education,0) AS eduid,
(COUNT(*)/ sumuser)*100 AS edupercent
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)
GROUP BY u.education
ORDER BY u.education;
END ;
-- 职业
WHEN 'pro'
THEN
BEGIN
SELECT
CASE
u.profession
WHEN 1
THEN N'學生'
WHEN 2
THEN N'科技業'
WHEN 3
THEN N'服務業'
WHEN 4
THEN N'製造業'
WHEN 5
THEN N'批發零售'
WHEN 6
THEN N'文創'
WHEN 7
THEN N'軍公教'
WHEN 8
THEN N'醫療'
WHEN 9
THEN N'其他'
ELSE N'(其他)'
END AS pro,
IFNULL(u.profession,0) AS proid,
(COUNT(*)/ sumuser)*100 AS propercent
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)
GROUP BY u.profession
ORDER BY u.profession;
END ;
-- 购买的书种
WHEN 'rec'
THEN
BEGIN
SELECT
COUNT(c.class_name) INTO sumuser
FROM
log_order_book AS lob
INNER JOIN log_order AS lo
ON lo.orderNo = lob.orderNo
INNER JOIN book_class bc
ON lob.book_seq = bc.book_seq
INNER JOIN class c
ON c.class_seq = bc.class_seq
AND lob.goods_type != '08'
AND lob.b_pay_status=1
AND c.class_name IN (
'商業理財',
'文學小說',
'藝術設計'
'心靈養生',
'休閒娛樂',
'圖文漫畫')
AND lo.o_est_datetime BETWEEN datefrom AND dateto;
SELECT
c.class_name AS rec,
IFNULL(c.class_seq,0) AS recid,
(COUNT(c.class_name)/sumuser)*100 AS recpercent
FROM
log_order_book AS lob
INNER JOIN log_order AS lo
ON lo.orderNo = lob.orderNo
INNER JOIN book_class bc
ON lob.book_seq = bc.book_seq
INNER JOIN class c
ON c.class_seq = bc.class_seq
AND lob.goods_type != '08'
AND lob.b_pay_status
AND c.class_name IN (
'商業理財',
'文學小說',
'藝術設計',
'心靈養生',
'休閒娛樂',
'圖文漫畫')
AND lo.o_est_datetime BETWEEN datefrom AND dateto
GROUP BY c.class_name;
END ;
END CASE ;
END$$
DELIMITER ;
调用存储过程:
CALL user_buy_analyze('edu','2011-01-01','2012-09-21')
注意事项:
ASP.NET中访问MySQL存储过程:
using MySql.Data.MySqlClient;
using System.Data.SqlClient;
public DataSet GetDataSet(string type,string DateFrom,string DateTo)
{
string connection = eBookConnection.connectionMySql;
MySqlConnection myCon = new MySqlConnection(eBookConnection.connectionMySql);
myCon.Open();
DataTable dt = new DataTable();
try
{
MySqlDataAdapter command = new MySqlDataAdapter();
DataSet ds = new DataSet();
command.SelectCommand = new MySqlCommand();
command.SelectCommand.Connection = myCon;
command.SelectCommand.CommandText = "user_buy_analyze";
command.SelectCommand.CommandType = CommandType.StoredProcedure;
MySqlParameter antype_para = new MySqlParameter("?antype", MySqlDbType.VarChar, 10);//mysql的存储过程参数是以?打头的
MySqlParameter datefrom_para = new MySqlParameter("?datefrom", MySqlDbType.VarChar, 50);
MySqlParameter dateto_para = new MySqlParameter("?dateto", MySqlDbType.VarChar, 50);
antype_para.Value = type;
datefrom_para.Value = DateFrom;
dateto_para.Value = DateTo;
command.SelectCommand.Parameters.Add(antype_para);
command.SelectCommand.Parameters.Add(datefrom_para);
command.SelectCommand.Parameters.Add(dateto_para);
command.Fill(ds);
return ds;
}
catch (Exception e)
{
throw e;
}
finally
{
myCon.Close();
}
}