IF with syntax error

Asked

Viewed 198 times

0

I’m trying to create a simple chat system using database but want to automatically when it reaches a limit of Rows in the database it automatically run the truncate.

The Trigger code I made was this:

BEGIN
    SELECT COUNT(*) INTO @a FROM sandbox_webchat;
    IF @a >= 120 THEN
        TRUNCATE sandbox_webchat;
    END IF
END

However he accuses IF and END IF as a syntax error, I even tested with parentheses but still he accuses error.

IF (@a >= 120) THEN ... END IF

One or more errors have occurred while Processing your request: The following query has failed: "CREATE TRIGGER QuandoAtingirLimite BEFORE INSERT ON sandbox_webchat FOR EACH ROW BEGIN SELECT COUNT(*) INTO @a FROM sandbox_webchat; IF @a >= 120 THEN TRUNCATE sandbox_webchat; END IF END"

Mysql said: #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 'END' at line 6

Erro do IF e ENDIF

  • In a chat, where the server can easily receive multiple messages per second, running a Count at all of these times is a bad idea (it will waste a lot of processing, overload the table, slow down the exchange of msgs in the chat). If you want to ensure storage space by cleaning old messages, consider creating a periodic event (run daily or in another specific time interval)

  • hm. thanks for the tip, but the IF still gives error. phpmyadmin tells me to check the syntax

  • 1

    In fact, you do not have this "IF" statement in mysql. What you have is SELECT IF, has a look at the documentation http://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html

  • Vish and now how I will create this condition.

1 answer

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.