发现美的眼睛 Prepared SQL Statement
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的更多相关文章
- 直接放个DB2 SQL STATEMENT大全好了!
SQL statements This topic contains tables that list the SQL statements classified by type. SQL sch ...
- Mysql--由prepared sql statement引发的问题
问题回顾 最近生产环境数据库查询接口异常,抛出异常信息表明预处理sql语句声明已经超过mysql系统设置限制max_prepared_stmt_count:通过网上一些资料,分析大概是程序中数据库查询 ...
- java.sql.preparedstatement和java.sql.statement的区别
本文转自CSDN,然后整理了一遍.原文出处:CSDN JDBC(java database connectivity,java数据库连接)的api中的主要的四个类之一的java.sql.stateme ...
- 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 ...
- MyBatis3: Could not find SQL statement to include with refid ‘
错误: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.Incompl ...
- 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 ...
- 什么是SQL statement?
什么是SQL statement? 1.SQL SELECT statement - SELECT命令 REFER: What is SQL, and what are some example st ...
- Viewing the Raw SQL Statement(xcode で)
Thanks to Core Data. Even without learning SQL and database, you’re able to perform create, select, ...
- 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. ...
随机推荐
- Java Hour 61 基础概念拾遗
循环遍历器 for (Iterator iter = list.iterator(); iter.hasNext();) { int i = ((Integer) iter.next()).intVa ...
- TP-Link 无线路由器设置图文教程----怎么设置TP-Link无线路由器图解
转自:http://www.jb51.net/softjc/39399.html 无线路由器的基础配置 在我们第一次配置无线宽带路由器时,参照说明书找到无线宽带路由器默认的IP地址是192.168.1 ...
- BLUEZ测试报告
测试小分队介绍: 马辰,李孟,罗凡 测试任务分类 帮助类 战斗类 道具类 敌军类 英雄类 地图类 AboutScene.h AchieveLayers.h GameLoading.h GameOver ...
- login控件重载登陆方法
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- OUYA设备的购买和安装
OUYA设备的购买和安装 在为OUYA这款游戏终端做具体的开发之前,建议读者先上手感受一下这款设备的游戏体验.本节所涉及的内容包括,OUYA设备的购买.安装.设置,以及最终上手开始游戏这一系列的过程本 ...
- C# 中的可变参数方法(VarArgs)
首先需要明确一点:这里提到的可变参数方法,指的是具有 CallingConventions.VarArgs 调用约定的方法,而不是包含 params 参数的方法.可以通过MethodBase.Call ...
- ural 1437. Gasoline Station
1437. Gasoline Station Time limit: 1.0 secondMemory limit: 64 MB Once a gasoline meter broke at a fi ...
- HDU 4417 (划分树+区间小于k统计)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给定一个区间,以及一个k值,求该区间内小于等于k值的数的个数.注意区间是从0开始的 ...
- TYVJ P1024 外星人的密码数字
做题记录:2016-08-16 20:09:30 描述 XXXX年突然有外星人造访,但大家语言不通,不过科学家们经过研究发现外星人用26个英文字母组成的单词中最长不降子序列的长度来表述数字,且 ...
- 1^b+2^b+3^b+...+n^b数列
首先,这是我自己推出来的,O(n^2),常数巨大.所以无能为力优化!所以求此数列的公式!求优化!!! 主要思想:要算b次的,那么就要先算b+1次的. 首先,我用F(i, j)表示杨辉三角第i层第j个, ...