DROP PROCEDURE IF EXISTS truncate_insert_sales_rank_toparow_month;
DELIMITER /w/
CREATE PROCEDURE truncate_insert_sales_rank_toparow_month ()
BEGIN
TRUNCATE sales_rank_toparow_month ;
INSERT INTO sales_rank_toparow_month (
fk_country,
fk_categoryid,
history_year,
history_month
) SELECT
country,
categoryid,
history_year,
history_month
FROM
grab_sales_rank_month
GROUP BY
country,
categoryid,
history_year,
history_month ;
END/w/
DELIMITER;
CALL truncate_insert_sales_rank_toparow_month; DROP PROCEDURE IF EXISTS insert_update_sales_rank_toparow_month;
DELIMITER /w/
CREATE PROCEDURE insert_update_sales_rank_toparow_month ()
BEGIN
SET @wtab = ' sales_rank_toparow_month al ';
SET @w = 10;
WHILE @w < 110 DO
SET @wfield = CONCAT('coin',@w);
SET @wnewvalue = CONCAT('(SELECT sum_coin FROM grab_sales_rank_month da WHERE al.fk_country = da.country AND al.fk_categoryid = da.categoryid AND al.history_year = da.history_year AND al.history_month = da.history_month AND da.topx=',@w,')');
SET @wfieldb = CONCAT('amount',@w);
SET @wnewvalueb = CONCAT('(SELECT sum_amount FROM grab_sales_rank_month da WHERE al.fk_country = da.country AND al.fk_categoryid = da.categoryid AND al.history_year = da.history_year AND al.history_month = da.history_month AND da.topx=',@w,' )');
SET @wpre = CONCAT('UPDATE ',@wtab,' SET ',@wfield,'=',@wnewvalue,',',@wfieldb,'=',@wnewvalueb);
PREPARE stmt FROM @wpre ;
EXECUTE stmt ;
DROP PREPARE stmt;
SET @w = @w + 10 ;
END
WHILE ;
END/w/
DELIMITER ;
CALL insert_update_sales_rank_toparow_month; DROP PROCEDURE IF EXISTS truncate_insert_sales_rank_toparow_week;
DELIMITER /w/
CREATE PROCEDURE truncate_insert_sales_rank_toparow_week ()
BEGIN
TRUNCATE sales_rank_toparow_week ;
INSERT INTO sales_rank_toparow_week (
fk_country,
fk_categoryid,
history_year,
history_week
) SELECT
country,
categoryid,
history_year,
history_week
FROM
grab_sales_rank_week
GROUP BY
country,
categoryid,
history_year,
history_week ;
END/w/
DELIMITER;
CALL truncate_insert_sales_rank_toparow_week; DROP PROCEDURE IF EXISTS insert_update_sales_rank_toparow_week;
DELIMITER /w/
CREATE PROCEDURE insert_update_sales_rank_toparow_week ()
BEGIN
SET @wtab = ' sales_rank_toparow_week al ';
SET @w = 10;
WHILE @w < 110 DO
SET @wfield = CONCAT('coin',@w);
SET @wnewvalue = CONCAT('(SELECT sum_coin FROM grab_sales_rank_week da WHERE al.fk_country = da.country AND al.fk_categoryid = da.categoryid AND al.history_year = da.history_year AND al.history_week = da.WeekValue AND da.topx=',@w,' )');
SET @wfieldb = CONCAT('amount',@w);
SET @wnewvalueb = CONCAT('(SELECT sum_amount FROM grab_sales_rank_week da WHERE al.fk_country = da.country AND al.fk_categoryid = da.categoryid AND al.history_year = da.history_year AND al.history_week = da.WeekValue AND da.topx=',@w,' )');
SET @wpre = CONCAT('UPDATE ',@wtab,' SET ',@wfield,'=',@wnewvalue,',',@wfieldb,'=',@wnewvalueb);
PREPARE stmt FROM @wpre ;
EXECUTE stmt ;
DROP PREPARE stmt;
SET @w = @w + 10 ;
END
WHILE ;
END/w/
DELIMITER ;
CALL insert_update_sales_rank_toparow_week; DROP PROCEDURE IF EXISTS truncate_insert_sales_rank_toparow_all;
DELIMITER /w/
CREATE PROCEDURE truncate_insert_sales_rank_toparow_all ()
BEGIN
TRUNCATE sales_rank_toparow_all ;
INSERT INTO sales_rank_toparow_all (
fk_country,
fk_categoryid
) SELECT
country,
categoryid
FROM
grab_sales_rank_all
GROUP BY
country,
categoryid;
END/w/
DELIMITER;
CALL truncate_insert_sales_rank_toparow_all; DROP PROCEDURE IF EXISTS insert_update_sales_rank_toparow_all;
DELIMITER /w/
CREATE PROCEDURE insert_update_sales_rank_toparow_all ()
BEGIN
SET @wtab = ' sales_rank_toparow_all al ';
SET @w = 10;
WHILE @w < 110 DO
SET @wfield = CONCAT('coin',@w);
SET @wnewvalue = CONCAT('(SELECT sum_coin FROM grab_sales_rank_all da WHERE al.fk_country = da.country AND al.fk_categoryid = da.categoryid AND da.topx=',@w,')');
SET @wfieldb = CONCAT('amount',@w);
SET @wnewvalueb = CONCAT('(SELECT sum_amount FROM grab_sales_rank_all da WHERE al.fk_country = da.country AND al.fk_categoryid = da.categoryid AND da.topx=',@w,')');
SET @wpre = CONCAT('UPDATE ',@wtab,' SET ',@wfield,'=',@wnewvalue,',',@wfieldb,'=',@wnewvalueb);
PREPARE stmt FROM @wpre ;
EXECUTE stmt ;
DROP PREPARE stmt;
SET @w = @w + 10 ;
END
WHILE ;
END/w/
DELIMITER ;
CALL insert_update_sales_rank_toparow_all;
 DROP PROCEDURE
IF EXISTS `prepare_update`;
DELIMITER /w/ CREATE PROCEDURE `prepare_update` ()
BEGIN SET @wtab = ' wtable ' ;
SET @w = 10 ;
WHILE @w < 40 DO SET @wfield = CONCAT('coin' ,@w) ;
SET @wnewvalue = @w + 1 ;
SET @wpre = CONCAT(
'UPDATE ' ,@wtab,
' SET ' ,@wfield,
'=' ,@wnewvalue
) ;
PREPARE stmt FROM @wpre ;
EXECUTE stmt ;
DROP PREPARE stmt ;
SET @w = @w + 10 ;
END
WHILE ;
END/w/
DELIMITER ; CALL `prepare_update`;

http://dev.mysql.com/doc/refman/5.7/en/sql-syntax-prepared-statements.html

 CREATE TABLE t1 (a INT NOT NULL);
INSERT INTO t1 VALUES (4), (8), (11), (32), (80);
SET @table = 't1';
SET @s = CONCAT('SELECT * FROM ', @table);
PREPARE stmt3 FROM @s;
EXECUTE stmt3;
 SET @s = 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';
PREPARE stmt2 FROM @s;
SET @a = 6;
SET @b = 8;
EXECUTE stmt2 USING @a, @b;
 PREPARE stmt1 FROM 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';
SET @a = 3;
SET @b = 4;
EXECUTE stmt1 USING @a, @b;
SHOW VARIABLES LIKE '%a%';

发现美的眼睛 Prepared SQL Statement的更多相关文章

  1. 直接放个DB2 SQL STATEMENT大全好了!

    SQL statements   This topic contains tables that list the SQL statements classified by type. SQL sch ...

  2. Mysql--由prepared sql statement引发的问题

    问题回顾 最近生产环境数据库查询接口异常,抛出异常信息表明预处理sql语句声明已经超过mysql系统设置限制max_prepared_stmt_count:通过网上一些资料,分析大概是程序中数据库查询 ...

  3. java.sql.preparedstatement和java.sql.statement的区别

    本文转自CSDN,然后整理了一遍.原文出处:CSDN JDBC(java database connectivity,java数据库连接)的api中的主要的四个类之一的java.sql.stateme ...

  4. Drop all the tables, stored procedures, triggers, constraints and all the dependencies in one SQL statement

    Is there any way in which I can clean a database in SQl Server 2005 by dropping all the tables and d ...

  5. MyBatis3: Could not find SQL statement to include with refid ‘

    错误: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.Incompl ...

  6. ORA-06550:line 1,column 7;PLS-00201:indentifer '存储过程' must be declared;...PL/SQL Statement ignored 问题

    前段时间由于修改SMES系统,出现了一个问题. ORA-06550:line 1,column 7;PLS-00201:indentifer '存储过程' must be declared;...PL ...

  7. 什么是SQL statement?

    什么是SQL statement? 1.SQL SELECT statement - SELECT命令 REFER: What is SQL, and what are some example st ...

  8. Viewing the Raw SQL Statement(xcode で)

    Thanks to Core Data. Even without learning SQL and database, you’re able to perform create, select, ...

  9. Caused by: org.h2.jdbc.JdbcSQLException: Table "T_STUDENT_INFO" not found; SQL statement

    1.错误描述 org.hibernate.exception.SQLGrammarException: error executing work at org.hibernate.exception. ...

随机推荐

  1. [译]SQL Server 之 索引基础

    SQL Server中,索引以B-tree的结构组织数据.B-tree代表平衡树,但是SQL Server使用一种叫做B+的树. B+树不是总是保持严格的平衡的树. 首先,索引有两个主要的部件:一个页 ...

  2. Struts2标签实现for循环

    感悟:但是不建议使用这种方法,按照MVC框架的思想 ,应该把业务更多放在后台.前台尽量只进行数据展示. 转自:http://blog.csdn.net/guandajian/article/detai ...

  3. CodeChef DISTNUM2 Easy Queries 节点数组线段树

    Description You are given an array A consisting of N positive integers. You have to answer Q queries ...

  4. 在js里面使用php语言

  5. n个元素进栈,共有多少种出栈顺序?

    1.基于栈的问题分析 我们把n个元素的出栈个数的记为f(n), 那么对于1,2,3, 我们很容易得出:                                   f(1) = 1     / ...

  6. BeagleBone硬件概览Ethernet端口板载LEDc重置按钮等介绍

    BeagleBone硬件概览Ethernet端口板载LEDc重置按钮等介绍 你进入BeagleBone世界的第一步就是将它连接以得到命令提示,然后你就可以处理文件以及执行命令了.在这里,你就可以定制你 ...

  7. 转 BHO API HOOK Wininet基于IE编程的一些资料

      BHO原理:推荐vc base中的文章: 如何使用BHO定制你的Internet Explorer浏览器 API HOOK的基本原理:推荐C++ builder 研究中的文章: API Hook基 ...

  8. windows 8 系统部署IIS并发布网站

    企业用户可以在已经部署了windows 8 的电脑中通过部署IIS服务器来发布自己公司的企业内部网站实现对企业的网络办公的管理工作. 准备篇 IIS的添加和运行 一.IIS的添加 1.请进入“控制面板 ...

  9. BZOJ2874 : 训练士兵

    设$a[i][j]$表示$(i,j)$右下角要增加多少 $aj[i][j]=a[i][j]\times j$ $ai[i][j]=a[i][j]\times i$ $aij[i][j]=a[i][j] ...

  10. vi/vim键盘图-----又一张桌面背景好图

    也许还是有很多人不能愿意用CLI的vi/Vim来写东西,不过,当你真的习惯了,它的高效性就是不可估量了.下面的这张图,一看就明白了,从此,学习变的不再艰难^_^ 补注: 图中没有关于查找和替换的,应该 ...