Pregunta

I was wondering if you can tell me what's wrong with the ff sql statement:

insert into translog 
  select * from transponder_logs where trans_log_id < 150000;
delete from transponder_logs where trans_log_id < 150000

This statement ran just fine in sql but it gives me a syntax error when I used it on event scheduler.

The error message was:

"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 'delete from transponder_logs where trans_log_id < 150000 at line 3"

¿Fue útil?

Solución

Whenever you define code like routines that have multiple executable statements, you MUST define a custom DELIMITER. And your code will be sent to the server along with delimiter instruction. And the server compiles the code as a block before it finds the newly defined custom delimiter.

Read what documentation says:
Defining Stored Programs

If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.

To redefine the mysql delimiter, use the delimiter command. .... The delimiter is changed to // to enable the entire definition to be passed to the server as a single statement, and then restored to ; before invoking the procedure. This enables the ; delimiter used in the procedure body to be passed through to the server rather than being interpreted by mysql itself.

I believe your event scheduler code is just executed as is without defining such delimiter.

Change it as following:

-- set the new delimiter
DELIMITER //

-- include your event scheduler code block here

-- lastly terminate the code block, with new delimiter
-- so that server starts compiling the code

//

-- now reset the delimiter to default
DELIMITER ;

Refer to: CREATE EVENT Syntax

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top