码迷,mamicode.com
首页 > 其他好文 > 详细

一个简单的存储过程范例

时间:2016-06-24 14:23:23      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

 1 delimiter $$
 2 CREATE PROCEDURE `proc_Ranking`(IN sku VARCHAR(20),IN skuname VARCHAR(20),IN status VARCHAR(20),IN sales VARCHAR(20),IN today VARCHAR(20),IN old_time VARCHAR(20))
 3 BEGIN
 4     SET @sku = sku;
 5     SET @skuname = skuname;
 6     SET @status = status;
 7     SET @sales = sales;
 8     SET @today = today;
 9     SET @old_time = old_time;
10     SET @sql_header = "SELECT
11              c.`sku`,c.`amt`,c.`avgs` avg,s.`skuname`,s.`skupic`,s.`edittime`,st.`statusname`
12         FROM(
13             SELECT
14                  `sku` , ROUND(SUM( `amt` ) , 2) amt , ROUND(SUM( `avg` ) /7 , 2) avgs
15             FROM `skusales`
16             WHERE    
17                  `sku` <> ‘‘
18             AND `paytime` > @old_time
19             AND `paytime` < @today
20             GROUP BY `sku`) c
21             LEFT JOIN
22                 `skustatus` s ON c.`sku` = s.`sku`
23             LEFT JOIN
24                 `statustype` st ON s.`sale_status` = st.`pid`
25         WHERE
26             1";
27     IF @sku <> ‘‘ THEN
28     SET @sql_header = CONCAT(@sql_header," AND c.`sku` = @sku");
29     END IF;
30 
31     IF @status <> ‘‘ THEN
32     SET @sql_header = CONCAT(@sql_header," AND s.`sale_status` = @status");
33     END IF;
34 
35     IF @skuname <> ‘‘ THEN
36     SET @sql_header = CONCAT(@sql_header," AND s.`skuname` like @skuname");
37     END IF;
38 
39     IF @sales <> ‘‘ THEN
40     SET @sql_header = CONCAT(@sql_header," AND c.avgs");
41     SET @sql_header = CONCAT(@sql_header,@sales);
42     END IF;
43     SET @sql_header = CONCAT(@sql_header," ORDER BY c.amt DESC");
44 
45     SET @sql_bodys = @sql_header;
46     
47     PREPARE stmt FROM @sql_bodys;
48     EXECUTE stmt;
49     DEALLOCATE PREPARE stmt ;
50         
51     END$$

 

一个简单的存储过程范例

标签:

原文地址:http://www.cnblogs.com/qq2317894314/p/5613788.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!