MySql MariaDB创建过程错误

时间:2017-03-31 08:55:25

标签: mysql stored-procedures mariadb

我正在为项目构建我的第一个mySQL数据库,将数据库从MS Access传输到mysql。现在我知道我有一个巨大的学习曲线但是没关系,我有一天必须学习它。

我正在XAMPP-phpMyAdmin的mySQL数据库中构建这个数据库。

我遇到的问题是创建一个过程,这是完整的过程: (由于我工作的自信性,一些表或变量名已被更改,因此无需评论Table1是一个坏名称等...,我已经知道了)

DELIMITER $$

DROP PROCEDURE IF EXISTS Proc1$$

CREATE PROCEDURE Proc1(IN pType INT)

BEGIN

SET @runningTotal=0, @prevTotal=0, @Aim=5, @Period=21;

DROP TEMPORARY TABLE IF EXISTS temp_table;


CREATE TEMPORARY TABLE IF NOT EXISTS temp_table(
  `id` int(11) NOT NULL,
  `dDate` date DEFAULT NULL,
  `type` int(11) DEFAULT NULL,
  `MyAmountCol` int(11) DEFAULT NULL
);

INSERT INTO temp_table(`id`, `dDate`, `type`, `MyAmountCol`) 
  SELECT `id`, `dDate`, `type`, `MyAmountCol` 
  FROM Table1 
  WHERE type=pType AND 
        table1.dDate>=DATE_SUB(CURDATE(), INTERVAL @Period DAY) AND
        table1.dDate!=CURDATE();

DROP TEMPORARY TABLE IF EXISTS temp_table_result;

CREATE TEMPORARY TABLE IF NOT EXISTS temp_table_result AS
  SELECT
    temp_table.Id,
    temp_table.dDate,
    temp_table.MyAmountCol,
    DATE_ADD(temp_table.dDate, INTERVAL @Period DAY) as 'RecDay',
    DATE_ADD(@prevDate, INTERVAL @Period DAY) As 'NextRecDay',
    @prevTotal := @runningTotal as 'PreviousTotal',
    @Aim -@prevTotal as 'MinAmmount',
    @runningTotal := @runningTotal + temp_table.MyAmountCol as 'RunningTotal',
    @prevDate := temp_table.dDate
    pType as 'Type'
  FROM temp_table 
  HAVING PreviousTotal <= @Aim
  ORDER BY RunningTotal DESC LIMIT 1;

UPDATE Result A INNER JOIN temp_table_result B (A.type = B.Type) 
  SET  A.RecDay=B.RecDay, A.NextRecDay=B.NextRecDay, A.MinHours=B.MinAmmount;

DROP TEMPORARY TABLE IF EXISTS temp_table;
DROP TEMPORARY TABLE IF EXISTS temp_table_result;

END;
$$
DELIMITER ;

所以问题是我花了一整天时间看着会出现以下错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'as 'Type'
FROM temp_table 
HAVING PreviousTotal <= @Aim
ORDER BY Runn' at line 37

我知道这是一个语法错误,我一直在慢慢地修复语法错误,(在你创建时学习的乐趣)。但我无法弄清楚这种语法错误。

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

试试以下代码。 希望这会有所帮助。

DROP PROCEDURE IF EXISTS Proc1;
DELIMITER $$
CREATE PROCEDURE Proc1(IN pType INT)

BEGIN

SET @runningTotal=0, @prevTotal=0, @Aim=5, @Period=21;

DROP TEMPORARY TABLE IF EXISTS temp_table;


CREATE TEMPORARY TABLE IF NOT EXISTS temp_table(
  `id` int(11) NOT NULL,
  `dDate` date DEFAULT NULL,
  `type` int(11) DEFAULT NULL,
  `MyAmountCol` int(11) DEFAULT NULL
);

INSERT INTO temp_table(`id`, `dDate`, `type`, `MyAmountCol`) 
  SELECT `id`, `dDate`, `type`, `MyAmountCol` 
  FROM Table1 
  WHERE type=pType AND 
        table1.dDate>=DATE_SUB(CURDATE(), INTERVAL @Period DAY) AND
        table1.dDate!=CURDATE();

DROP TEMPORARY TABLE IF EXISTS temp_table_result;

CREATE TEMPORARY TABLE IF NOT EXISTS temp_table_result AS
  SELECT
    temp_table.Id,
    temp_table.dDate,
    temp_table.MyAmountCol,
    DATE_ADD(temp_table.dDate, INTERVAL @Period DAY) as 'RecDay',
    DATE_ADD(@prevDate, INTERVAL @Period DAY) As 'NextRecDay',
    @prevTotal := @runningTotal as 'PreviousTotal',
    @Aim -@prevTotal as 'MinAmmount',
    @runningTotal := @runningTotal + temp_table.MyAmountCol as 'RunningTotal',
    @prevDate := temp_table.dDate, <--- Make change at this line.
    pType as 'Type'
  FROM temp_table 
  HAVING PreviousTotal <= @Aim
  ORDER BY RunningTotal DESC LIMIT 1;

UPDATE Result A INNER JOIN temp_table_result B ON(A.type = B.Type) 
  SET  A.RecDay=B.RecDay, A.NextRecDay=B.NextRecDay, A.MinHours=B.MinAmmount;

DROP TEMPORARY TABLE IF EXISTS temp_table;
DROP TEMPORARY TABLE IF EXISTS temp_table_result;

END;
$$
DELIMITER ;