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. mac上安装gradle

    首先,先download最新版本的gradle,网址如下:http://www.gradle.org/get-started然后将下载下来的zip包放在你要安装的路径上,我安装在/usr/local/ ...

  2. SAE Java相关问题小结

    转自:http://blog.csdn.net/bhq2010/article/details/8580412 Sae中使用的servlet容器是jetty7.4.x 我想在web.xml中配置一个自 ...

  3. Struts2标签实现for循环

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

  4. Android判断App是否在前台运行(转)

    原文地址: http://blog.csdn.net/zuolongsnail/article/details/8168689 Android开发中,有时候需要判断App是否在前台运行. 代码实现如下 ...

  5. HDU 4345 Permutation dp

    Permutation Problem Description There is an arrangement of N numbers and a permutation relation that ...

  6. C#学习笔记(一)——HelloWorld!

    一.平台的搭建(IDE) 使用的VS2013,不知道是不是微软--(省略N多字),注册只要简单的KEY,这个我们可以直接度娘一大堆,所以不用担心这个软件安不上去= = 建议使用2013社区版,官方免费 ...

  7. SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用 'Ad Hoc Distributed Queries'。有关启用 'Ad Hoc Distributed Que

    看错误提示就知道是因为SQL Server的Ad Hoc Distributed Queries组件被禁用了,这里我用的SQL Server版本是2005,只需要开启Ad Hoc Distribute ...

  8. SQL Server连接数据库失败,可能的问题!

    SQL Server Configuration Manager中启动服务 SQL Server外围应用配置器中,打开远程IP连接属性 别的应该没什么问题了!

  9. exec命令

    exec 命令实例 find . -name "*.cc" -exec grep -P -n -H --color=auto "[^\w]main[^\w]" ...

  10. LinQ的一些基本语句

    LINQ(Language Integrated Query)即语言集成查询.它是一种语言特性和API,使得你可以使用统一的方式编写各种查询,查询的对象包括xml.对象集合.SqlServer数据库等 ...