存储过程参数在concat

时间:2015-07-27 06:20:31

标签: mysql

下面是我正在使用的程序。我无法在下面提供的连接语句中使用过程的参数(ptodate timestamp,pfromdate timestamp)。请建议如何使用以下过程中的过程参数从开始和结束获取数据参数中提供的日期。当我运行此过程时,错误会生成“查询已终止”。在我在concat语句中使用(ptodate timestamp,pfromdate timestamp)参数的行发生错误。请建议。

CREATE DEFINER=`root `@`127.0.0.1` PROCEDURE`procedurename`(phonecode int,ptodate timestamp,pfromdate timestamp)
BEGIN
if(phonecode=880)
then
SET @sql = NULL;
SELECT
 GROUP_CONCAT(DISTINCT
 CONCAT(
 'SUM(IF(a.category_name = ''',
 a.category_name,
 ''',1,0)) AS ',
 "'",p.name,"'"
 )
 )into @SQL
 FROM tablea a 
JOIN tableb p ON a.category_name=p.id
where a.sender_country_code=880
and a.created_datetime>ptodate and a.created_datetime<pfromdate;

SET @sql = CONCAT('SELECT DATE(a.date_time),
', @sql, ' 
FROM edr_im a 
JOIN package p ON a.category_name=p.id
WHERE a.message_type="sticker"
and 
a.sender_country_code=880 and a.created_datetime>',ptodate,' and    a.created_datetime<',pfromdate,'
GROUP BY 1');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
end if;
end

1 个答案:

答案 0 :(得分:0)

您需要在日期周围加上引号。

SET @sql = CONCAT('SELECT DATE(a.date_time),
', @sql, ' 
FROM edr_im a 
JOIN package p ON a.category_name=p.id
WHERE a.message_type="sticker"
and 
a.sender_country_code=880 and a.created_datetime>"',ptodate,'" and    a.created_datetime<"',pfromdate,'"
GROUP BY 1');