简体   繁体   中英

MySql - Stored function syntax error

SQL:

CREATE FUNCTION delete_user (uId INT) RETURNS BOOLEAN
BEGIN
    IF uID >= 0 THEN
        START TRANSACTION;
        DELETE FROM Folder WHERE u_id = uId;
        DELETE FROM `User` WHERE id = uId;
        RETURN 1;
    END IF;
    RETURN 0;
END;

Error:

#1064 - 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 '' at line 4

Where is the syntax error?

You need to put a delimiter around your function

delimiter |
CREATE FUNCTION delete_usr (uId INT) RETURNS BOOLEAN
BEGIN
    IF uID >= 0 THEN        
        DELETE FROM Folder WHERE u_id = uId;
        DELETE FROM `User` WHERE id = uId;
        RETURN 1;
    END IF;
    RETURN 0;
END
|
delimiter ;

The default delimiter is ; . But if you want to create the procedure/function the DB engine would think your statement ends at the first ; . That would lead to an incomplete statement. You can define another delimiter and then use it to mark the end of statements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM