MySQL create table 语句的过程语法错误

MySQL procedure syntax error with create table statement

下面的代码在没有 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 ;

它给出的错误非常标准,即

Error Code: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'create table rc_pending_tat_temp SELECT IF(b.retailer IS NULL,a.retailer, b.ret' at line 4

在我看来 SQL 不喜欢您的查询试图创建一个空的 table。

我刚刚在 google 上四处搜索,看看是否可以创建一个没有任何列的 table,看起来这通常不是一件容易的事。如果您出于某些特定原因想要创建一个空的 table,那似乎是另一回事。但是如果你想解决这个问题,请至少在 table 中添加一列。像这样:

CREATE TABLE rc_pending_tat_temp(Id INT);

那应该得到你的查询 运行。

编辑:

听起来您想创建一个新的 table 来存储 SELECT 语句的结果。为此,请查看 SELECT INTO 语句 here 的语法。这将允许您根据 SELECT 语句的结果创建 table。

在这种情况下,您应该删除此行:

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 ;

请删除 BEGIN 之后的“(”和存储过程的 GROUP BY 1,2 部分之后的“)”。我在没有开始和结束括号的情况下尝试了这个,它对我有用。

您好,我通过很好的老式点击尝试方法发现了这个问题,结果是那些额外的括号 () 导致了语法错误。删除它们,代码就像魅力一样工作!

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 ;