使用 "IF" 程序无效

Procedure not working using "IF"

我有以下程序:

DELIMITER $$
CREATE OR REPLACE PROCEDURE add_comments
AS
BEGIN

DECLARE var INT;
SELECT grade INTO var FROM table1;

    IF (var <= 7) THEN
        UPDATE table2 set comment = not_good;
    ELSE
        UPDATE table2 set comment = good;
    END IF;
END$$

comment, not_good 和 good 都是来自 table2

的列

但它给了我错误:

#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
BEGIN

    IF (var <= 7) 
    BEGIN
        UPDATE table2 set comment ' at line 3

找不到问题,有人可以帮我解决这个问题吗? 谢谢!

从您的过程代码主体中删除 AS 运算符。你的程序应该看起来像

DELIMITER $$
CREATE OR REPLACE PROCEDURE add_comments
BEGIN
DECLARE var INT;
SELECT grade INTO var FROM table1;

    IF (var <= 7) THEN
        UPDATE table2 set comment = not_good;
    ELSE
        UPDATE table2 set comment = good;
    END IF;
END$$

参考MySQL Documentation了解更多信息

替换

DELIMITER $$
CREATE OR REPLACE PROCEDURE add_comments
AS
BEGIN

DELIMITER $$
CREATE OR REPLACE PROCEDURE add_comments()
BEGIN

PS: 添加括号并删除 "AS"