声明变量时MySQL存储过程中的语法错误

时间:2014-03-30 20:47:19

标签: mysql sql stored-procedures

我正在尝试创建一个过程,根据其他几个表自动向表中添加一些值。

这些是我的表格:

ENROLLMENT表(我存储学生注册的课程):

+----------------+---------+------+-----+---------+----------------+
| Field          | Type    | Null | Key | Default | Extra          |
+----------------+---------+------+-----+---------+----------------+
| ENROLMENT_NR   | int(10) | NO   | PRI | NULL    | auto_increment |
| ENROLMENT_DATE | date    | NO   |     | NULL    |                |
| STUDENT_NR     | int(10) | NO   | MUL | NULL    |                |
| PROGRAMME_NR   | int(10) | NO   | MUL | NULL    |                |
+----------------+---------+------+-----+---------+----------------+

PROGRAMME_MODULE表(列出了程序和各个模块组合的连接表):

+--------------+---------+------+-----+---------+-------+
| Field        | Type    | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+-------+
| PROGRAMME_NR | int(10) | NO   | PRI | NULL    |       |
| MODULE_NR    | int(10) | NO   | PRI | NULL    |       |
+--------------+---------+------+-----+---------+-------+

注册表(我希望将学生的注册存储到他们注册的每个课程的模块中):

+-------------------+---------+------+-----+---------+----------------+
| Field             | Type    | Null | Key | Default | Extra          |
+-------------------+---------+------+-----+---------+----------------+
| REGISTRATION_NR   | int(10) | NO   | PRI | NULL    | auto_increment |
| REGISTRATION_DATE | date    | NO   |     | NULL    |                |
| MODULE_NR         | int(10) | NO   | MUL | NULL    |                |
| STUDENT_NR        | int(10) | NO   | MUL | NULL    |                |
+-------------------+---------+------+-----+---------+----------------+

我之前从未在MySQL中完成过程。我只使用PL / SQL做了一些基本的。根据一些研究,我创建了这个程序:

DROP PROCEDURE IF EXISTS REGISTER_STUDENTS_PROC;
DELIMITER ;;
CREATE PROCEDURE REGISTER_STUDENTS_PROC()
DECLARE st_nr INT DEFAULT 0;
DECLARE prg_nr INT DEFAULT 0;
DECLARE enrl_date DATE DEFAULT '0000-00-00';
DECLARE mod_nr INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
DECLARE n INT DEFAULT 0;
DECLARE ii INT DEFAULT 0;
DECLARE nn INT DEFAULT 0;
SELECT COUNT(*) FROM TABLE ENROLMENT INTO n;
SET i=0;
SELECT COUNT(*) FROM TABLE PROGRAMME_MODULE INTO nn;
SET ii=0;
BEGIN
WHILE i<n DO
    SELECT STUDENT_NR FROM TABLE ENROLMENT INTO st_nr;
    SELECT PROGRAMME_NR FROM TABLE ENROLMENT INTO prg_nr;
    SELECT ENROLMENT_DATE FROM TABLE ENROLMENT INTO enrl_date;
    BEGIN
    WHILE ii < n DO
        SELECT MODULE_NR FROM TABLE PROGRAMME_MODULE WHERE PROGRAMME_NR = prg_nr INTO mod_nr;
        INSERT INTO REGISTRATION (REGISTRATION_DATE,MODULE_NR,STUDENT_NR) VALUES (enrl_date,mod_nr,st_nr);
        SET ii = ii + 1;
    END WHILE;
    SET i = i + 1;
END WHILE;
END;
;;

这是我的错误:

ERROR 1064 (42000): 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 'DECLARE st_nr INT DEFAULT 0;
DECLARE prg_nr INT DEFAULT 0; DECLARE enrl_date DAT' at line 2.

我在SQL中发现的一个难点是,某些错误非常不明确,这对初学者构成了额外的挑战......这可能是一些基本但我看不到的错误。

谢谢

1 个答案:

答案 0 :(得分:4)

根据@Mihai的评论,这里是没有语法错误的代码:

DROP PROCEDURE IF EXISTS REGISTER_STUDENTS_PROC;
DELIMITER ;;
CREATE PROCEDURE REGISTER_STUDENTS_PROC()
BEGIN
DECLARE st_nr INT DEFAULT 0;
DECLARE prg_nr INT DEFAULT 0;
DECLARE enrl_date DATE DEFAULT '0000-00-00';
DECLARE mod_nr INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
DECLARE n INT DEFAULT 0;
DECLARE ii INT DEFAULT 0;
DECLARE nn INT DEFAULT 0;
SELECT COUNT(*) FROM ENROLMENT INTO n;
SET i=0;
SELECT COUNT(*) FROM PROGRAMME_MODULE INTO nn;
SET ii=0;
WHILE i<n DO
    SELECT STUDENT_NR FROM ENROLMENT INTO st_nr;
    SELECT PROGRAMME_NR FROM ENROLMENT INTO prg_nr;
    SELECT ENROLMENT_DATE FROM ENROLMENT INTO enrl_date;
    BEGIN
    WHILE ii < n DO
        SELECT MODULE_NR FROM PROGRAMME_MODULE WHERE PROGRAMME_NR = prg_nr INTO mod_nr;
        INSERT INTO REGISTRATION (REGISTRATION_DATE,MODULE_NR,STUDENT_NR) VALUES (enrl_date,mod_nr,st_nr);
        SET ii = ii + 1;
    END WHILE;
    END;
    SET i = i + 1;
END WHILE;
END;
;;

所以,有三个更正:

1-必须在BEGIN之后声明变量; 2-必须使用BEGIN关闭所有END语句(嵌套的语句未关闭); 3-必须使用SELECT FROM <<TABLE_NAME>>而不是SELECT FROM TABLE <<TABLE_NAME>>