tags:

views:

30

answers:

1
Create EVENT event_test_name ON SCHEDULE EVERY 1 MINUTE STARTS '2010-09-02 12:10:15' ON COMPLETION NOT PRESERVE ENABLE 
DO 
SET Total=-1;
SET Total = (SELECT COUNT(0) FROM schema1.table1);
SELECT Total;
 IF Total >50000 THEN
insert into schema2.table1 (column1) 
select schema1.table1.column1 from schema1.table1;
end if;

This is the event scheduler i am writing for mysql, but i keep getting incorrect syntax line near if statemnet, any help will be highly helpful.

Thanks.

+1  A: 

You have to preface your variables with @, otherwise MySQL will assume they're system variables (and Total isn't one of those) or column names in a table, so...

...
SET @Total=-1;
SET @Total = (SELECT ...);
SELECT @Total;
IF @Total > ...

and so on.

Marc B
Yes i tried that too, i gotError Code : 1064You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF @Total >50000 THENinsert into schema2.table1 (column1) select schema1.table' at line 1
Sharpeye500
also, i think `:=` should be used (instead of `=`).
zerkms
PS: Am running these queries from SQL Yog.
Sharpeye500
@zerkms - when i try ":" i get incorrect syntax error.
Sharpeye500
`SET @Total := -1;` does not work for you?
zerkms
@sharpeye500: i've been working with mysql over 7 years and I have been **always** using such syntax since 3.23 to current 5.1. I can't believe it does not work.
zerkms
oops sorry zerkms, i tried again, it accepts := and = , but that doesn't solve my issue, am struck with this error: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 'IF @Total >50000 THENError Code : 1064You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'end if' at line 1
Sharpeye500
and that means that you have an error **right before** `IF`-clause.
zerkms
Are you sure you can do SELECTS within an IF statement? My reading of the IF man page doesn't seem to indicate you can: http://dev.mysql.com/doc/refman/5.0/en/if-statement.html
Marc B
If count > some value //doing insert stmnt(inst statement picks value by selecting another table;End if;How to achieve this?
Sharpeye500
@zerkms - Yes somewhere its failing i am not sure whether if statement or line before if statement.
Sharpeye500
@Sharp: Instead of doing this all within the scheduler setup, create a stored procedure to do it, and have the scheduler call that procedure.
Marc B