这是我认为Procedure语句的代码,我认为它可以正常运行,但是由于不断出现以下错误而无法运行它:错误代码:1064。您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册以获取在')附近使用的正确语法; -发放新贷款。在第70行插入VA'贷款(code
,no
,已到期)
我尝试找到修复它的方法,但没有找到任何东西。谁能看到我写错了什么,或者在代码中看不见的地方犯了错误吗?错误使我进入第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 ;
答案 0 :(得分:0)
MySQL Workbench给您一条错误消息,其中包含详细信息。你没看到吗实际上,它会给您2个错误,但是第一个(大约是FETCH NEXT)是WB语法检查器中的错误,将在下一版本中得到解决。
第二个说:
“ DATE_ADD”在此位置无效,需要完整的函数调用或其他表达式。
比MySQL(或MariaDB)发出的丑陋错误消息更好地描述了哪里出了问题。当您查看该呼叫时,您会看到在圆括号前有一个空格,该空格由于SQL mode IGNORE_SPACE而改变了含义。删除空格字符,您的SP将被接受。