0

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?

3
  • START|BEGIN TRANSACTION with no COMMIT? Commented Sep 19, 2013 at 10:15
  • if auto_commit is on then you don't need to do start transaction; Commented Sep 19, 2013 at 10:19
  • I'can't use START_TRANSACTION in a function? Commented Sep 19, 2013 at 10:37

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I removed "START_TRANSACTON" and added the delimiter. But still get the same error?
@Gabriel: I get no error when I try it. I added the complete function in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.