-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`advancebooker`@`%` PROCEDURE `make_intervals`(startdate datetime, enddate datetime, intval integer, unitval varchar(10), tmpTableName varchar(100))
BEGIN
-- *************************************************************************
--
-- Description:
-- This procedure creates a temporary table named time_intervals with the
-- interval_start and interval_end fields specifed from the startdate and
-- enddate arguments, at intervals of intval (unitval) size.
-- *************************************************************************
declare thisDate datetime;
declare nextDate datetime;
declare id INT;
declare vehicleCount int default 0;
declare productId int default 0;
set thisDate = startdate;
-- *************************************************************************
-- Drop / create the table
-- *************************************************************************
SET @sql = CONCAT('CREATE TABLE IF NOT EXISTS ',tmpTableName, '(id INT(11) NOT NULL AUTO_INCREMENT, interval_start DATETIME, interval_end DATETIME, vehicleCount INT(20), productId INT(10), PRIMARY KEY (id))');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- *************************************************************************
-- Loop through the startdate adding each intval interval until enddate
-- *************************************************************************
repeat
select
case unitval
when 'MICROSECOND' then timestampadd(MICROSECOND, intval, thisDate)
when 'SECOND' then timestampadd(SECOND, intval, thisDate)
when 'MINUTE' then timestampadd(MINUTE, intval, thisDate)
when 'HOUR' then timestampadd(HOUR, intval, thisDate)
when 'DAY' then timestampadd(DAY, intval, thisDate)
when 'WEEK' then timestampadd(WEEK, intval, thisDate)
when 'MONTH' then timestampadd(MONTH, intval, thisDate)
when 'QUARTER' then timestampadd(QUARTER, intval, thisDate)
when 'YEAR' then timestampadd(YEAR, intval, thisDate)
end into nextDate;
SET @sql = CONCAT("INSERT INTO ",tmpTableName," SELECT ", id, thisDate,TIMESTAMPADD(MICROSECOND, -1, nextDate), vehicleCount , productId);
SET thisDate = nextDate;
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
until thisDate >= enddate
end repeat;
END
When i debug the above stored proc in the SET @sql = CONCAT("INSERT INTO ",tmpTableName," ... line I am getting null value?
请告诉我错误的位置? 并且错误似乎在第一行 错误代码:1064。您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第1行的“NULL”附近使用正确的语法
答案 0 :(得分:0)
如果任何参数为NULL,则CONCAT()返回NULL。
此示例演示了这一点:http://sqlfiddle.com/#!2/d41d8/47037
您可以在此处找到有关使用空值的一些文档:http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html