我在 mySQL workbench 中创建过程语句时出现错误 1064
Error 1064 when i creating my Procedure Statement in mySQL workbench
这是我的程序语句代码,我认为它工作正常但我不能运行它因为我不断收到此错误:错误代码:1064。你有一个错误在你的 SQL 语法中;查看与您的 MariaDB 服务器版本对应的手册,了解在 ') 附近使用的正确语法。 ——发放新贷款。 INSERT INTO loan (code
, no
, taken, due) VA' at line 70
我试图找到修复它的方法,但我没有找到任何东西。其他人可以看到我是否写错了什么或者我在代码中的某个地方犯了我看不到的错误吗?错误是将我发送到第 70 行,但 FETCH NEXT FROM copy_c INTO copy_code;
告诉我 FROM
不在正确的位置。我还包括我的评论,以便更轻松地阅读此代码。
DELIMITER $$
CREATE PROCEDURE `new_loan` (IN book_isbn CHAR(17), IN student_no INT)
BEGIN
-- search for the copy of the book and test for loan record in loan table.
DECLARE copy_code, loan_test INT;
-- test for successful loan and for end of cursor.
DECLARE inserted, complete BOOLEAN;
-- the duration date and current date for new loan issue and number of days for the new loan.
DECLARE due, cur DATE;
DECLARE copy_dur TINYINT;
-- test if students can loan books.
DECLARE embargo_status BIT(1) DEFAULT b'1';
-- cursor for copy codes based on isbn.
DECLARE copy_c CURSOR FOR
SELECT `code`
FROM copy
WHERE isbn = book_isbn;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET complete = TRUE;
OPEN copy_c;
-- get student embargo status.
SET embargo_status = (SELECT embargo
FROM student
WHERE `no` = student_no);
SELECT embargo_status;
-- check if the student is valid or embargo is no, if not report a message.
IF (embargo_status IS NULL OR embargo_status = b'1') THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'The student is not valid or has embargo!';
END IF;
SET inserted = FALSE;
SET copy_code = 0;
-- loop through copies to see when that is available.
copy_codes: LOOP
FETCH NEXT FROM copy_c INTO copy_code;
IF complete THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'We looped to end and found nothing!';
END IF;
-- if a copy is available then loan_test will be null.
-- if return is null the book is out on loan and a non null value will be returned to loan_test.
SET loan_test = (SELECT `code` FROM loan
WHERE (`code` = copy_code) AND (`return` IS NULL));
-- if a copy is available loan_test will be null.
-- A null value implies that the copy had a one or many records in loan with a non null return or the copy was never out on loan.
IF (loan_test IS NULL) THEN
SET cur = CURRENT_DATE();
SET copy_dur = (SELECT duration
FROM copy
WHERE `code` = copy_code);
-- calculate due date.
SET due = DATE_ADD (cur, INTERVAL copy_dur DAY);
-- issue the new loan.
INSERT INTO loan (`code`, `no`, taken, due)
VALUES (copy_code, student_no, cur, due);
SET inserted = TRUE;
LEAVE copy_codes;
END IF;
END LOOP;
CLOSE copy_c;
-- inform users of a failed loan.
IF (inserted = FALSE) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'No currently available copies or book does not exist! ';
END IF;
END$$
DELIMITER ;
MySQL Workbench 为您提供错误消息和详细信息。你没看到吗?实际上,它给了你 2 个错误,但第一个(关于 FETCH NEXT)是 WB 语法检查器中的一个错误,将在下一个版本中修复。
第二个说:
"DATE_ADD" is not valid at this position, expecting a complete function call or other expression.
这比来自 MySQL(或 MariaDB 就此而言)的丑陋错误消息更好地描述了问题所在。当您查看该调用时,您会看到左括号前有一个 space,由于 SQL mode IGNORE_SPACE 而改变了含义。删除 space 字符,您的 SP 将被接受。
这是我的程序语句代码,我认为它工作正常但我不能运行它因为我不断收到此错误:code
, no
, taken, due) VA' at line 70
我试图找到修复它的方法,但我没有找到任何东西。其他人可以看到我是否写错了什么或者我在代码中的某个地方犯了我看不到的错误吗?错误是将我发送到第 70 行,但 FETCH NEXT FROM copy_c INTO copy_code;
告诉我 FROM
不在正确的位置。我还包括我的评论,以便更轻松地阅读此代码。
DELIMITER $$
CREATE PROCEDURE `new_loan` (IN book_isbn CHAR(17), IN student_no INT)
BEGIN
-- search for the copy of the book and test for loan record in loan table.
DECLARE copy_code, loan_test INT;
-- test for successful loan and for end of cursor.
DECLARE inserted, complete BOOLEAN;
-- the duration date and current date for new loan issue and number of days for the new loan.
DECLARE due, cur DATE;
DECLARE copy_dur TINYINT;
-- test if students can loan books.
DECLARE embargo_status BIT(1) DEFAULT b'1';
-- cursor for copy codes based on isbn.
DECLARE copy_c CURSOR FOR
SELECT `code`
FROM copy
WHERE isbn = book_isbn;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET complete = TRUE;
OPEN copy_c;
-- get student embargo status.
SET embargo_status = (SELECT embargo
FROM student
WHERE `no` = student_no);
SELECT embargo_status;
-- check if the student is valid or embargo is no, if not report a message.
IF (embargo_status IS NULL OR embargo_status = b'1') THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'The student is not valid or has embargo!';
END IF;
SET inserted = FALSE;
SET copy_code = 0;
-- loop through copies to see when that is available.
copy_codes: LOOP
FETCH NEXT FROM copy_c INTO copy_code;
IF complete THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'We looped to end and found nothing!';
END IF;
-- if a copy is available then loan_test will be null.
-- if return is null the book is out on loan and a non null value will be returned to loan_test.
SET loan_test = (SELECT `code` FROM loan
WHERE (`code` = copy_code) AND (`return` IS NULL));
-- if a copy is available loan_test will be null.
-- A null value implies that the copy had a one or many records in loan with a non null return or the copy was never out on loan.
IF (loan_test IS NULL) THEN
SET cur = CURRENT_DATE();
SET copy_dur = (SELECT duration
FROM copy
WHERE `code` = copy_code);
-- calculate due date.
SET due = DATE_ADD (cur, INTERVAL copy_dur DAY);
-- issue the new loan.
INSERT INTO loan (`code`, `no`, taken, due)
VALUES (copy_code, student_no, cur, due);
SET inserted = TRUE;
LEAVE copy_codes;
END IF;
END LOOP;
CLOSE copy_c;
-- inform users of a failed loan.
IF (inserted = FALSE) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'No currently available copies or book does not exist! ';
END IF;
END$$
DELIMITER ;
MySQL Workbench 为您提供错误消息和详细信息。你没看到吗?实际上,它给了你 2 个错误,但第一个(关于 FETCH NEXT)是 WB 语法检查器中的一个错误,将在下一个版本中修复。
第二个说:
"DATE_ADD" is not valid at this position, expecting a complete function call or other expression.
这比来自 MySQL(或 MariaDB 就此而言)的丑陋错误消息更好地描述了问题所在。当您查看该调用时,您会看到左括号前有一个 space,由于 SQL mode IGNORE_SPACE 而改变了含义。删除 space 字符,您的 SP 将被接受。