下面的代码在没有create table语句的情况下完美运行。我花了一个小时看着这段简单的代码来找出错误。
DELIMITER $$
USE `operations`$$
DROP PROCEDURE IF EXISTS `rc_pending_data_tat_proc`$$
CREATE PROCEDURE `rc_pending_data_tat_proc`()
BEGIN
(
CREATE TABLE rc_pending_tat_temp /*works fine when I remove this*/
SELECT IF(b.retailer IS NULL,a.retailer, b.retailer) AS Retailer,
(CASE
WHEN DATEDIFF(criteria_date,transaction_date)<=50
THEN '<=50'
WHEN DATEDIFF(criteria_date,transaction_date) <=70
THEN '<=70'
WHEN DATEDIFF(criteria_date,transaction_date) <=80
THEN '<=80'
WHEN DATEDIFF(criteria_date,transaction_date) <=90
THEN '<=90'
ELSE
'>90'
END) AS Pending_since
, COUNT(*) AS `count`, CURRENT_TIMESTAMP AS `date`
FROM `rc_pending_data` a
LEFT JOIN `rc_store_retailer_mapping` b
ON a.retailer=b.store_name
GROUP BY 1,2
);
END$$
DELIMITER ;
它给出的错误非常标准,即
错误代码:1064 您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以便在'create table rc_pending_tat_temp附近使用正确的语法 SELECT IF(b.retailer IS NULL,a.retailer,b.ret'在第4行
答案 0 :(得分:1)
请在存储过程的 GROUP BY 1,2 部分之后删除“( BEGIN 和”之后的“)”。我尝试了这个没有开始和结束括号,它对我有用。
答案 1 :(得分:0)
在我看来,SQL并不喜欢你的查询试图创建一个空表的事实。
我只是用Google搜索,看看你是否可以创建一个没有任何列的表格,看起来它通常不是一件容易的事情。如果您想出于某些特定原因创建一个空表,那似乎是另一回事。但是,如果要解决此问题,请在表中添加至少一列。像这样:
CREATE TABLE rc_pending_tat_temp(Id INT);
这应该让你的查询运行。
修改强>
听起来您想要创建一个新表来存储SELECT
语句的结果。为此,请查看SELECT INTO
语句here的语法。这样您就可以根据SELECT
语句的结果创建一个表格。
在这种情况下,您应该删除此行:
CREATE TABLE rc_pending_tat_temp
然后添加:
INTO rc_pending_tat_temp
所以你的查询应该看起来像这样:
DELIMITER $$
USE `operations`$$
DROP PROCEDURE IF EXISTS `rc_pending_data_tat_proc`$$
CREATE PROCEDURE `rc_pending_data_tat_proc`()
BEGIN
(
SELECT IF(b.retailer IS NULL,a.retailer, b.retailer) AS Retailer,
(CASE
WHEN DATEDIFF(criteria_date,transaction_date)<=50
THEN '<=50'
WHEN DATEDIFF(criteria_date,transaction_date) <=70
THEN '<=70'
WHEN DATEDIFF(criteria_date,transaction_date) <=80
THEN '<=80'
WHEN DATEDIFF(criteria_date,transaction_date) <=90
THEN '<=90'
ELSE
'>90'
END) AS Pending_since
, COUNT(*) AS `count`, CURRENT_TIMESTAMP AS `date`
INTO rc_pending_tat_temp
FROM `rc_pending_data` a
LEFT JOIN `rc_store_retailer_mapping` b
ON a.retailer=b.store_name
GROUP BY 1,2
);
END$$
DELIMITER ;
答案 2 :(得分:0)
您好我通过良好的旧命中和尝试方法找到了问题,结果发现那些额外的括号()导致了语法错误。删除它们,代码就像魅力一样!
DELIMITER $$
USE `operations`$$
DROP PROCEDURE IF EXISTS `rc_pending_data_tat_proc`$$
CREATE PROCEDURE `rc_pending_data_tat_proc`()
BEGIN
(
CREATE TABLE rc_pending_tat_temp /*works fine when I remove this*/
SELECT IF(b.retailer IS NULL,a.retailer, b.retailer) AS Retailer,
(CASE
WHEN DATEDIFF(criteria_date,transaction_date)<=50
THEN '<=50'
WHEN DATEDIFF(criteria_date,transaction_date) <=70
THEN '<=70'
WHEN DATEDIFF(criteria_date,transaction_date) <=80
THEN '<=80'
WHEN DATEDIFF(criteria_date,transaction_date) <=90
THEN '<=90'
ELSE
'>90'
END) AS Pending_since
, COUNT(*) AS `count`, CURRENT_TIMESTAMP AS `date`
FROM `rc_pending_data` a
LEFT JOIN `rc_store_retailer_mapping` b
ON a.retailer=b.store_name
GROUP BY 1,2
);
END$$
DELIMITER ;