简体   繁体   中英

MYSQL syntax delimiter and syntax error

I have tried to fix the syntax errors in the following but I can't see what on earth is wrong here:

DELIMITER =
CREATE TRIGGER trigs BEFORE UPDATE ON autoinc 
FOR EACH ROW BEGIN 
DECLARE num_rows INTEGER; 
SELECT (*) INTO num_rows FROM autoinc;
IF num_rows >=3 THEN 
DELETE FROM autoinc LIMIT 1; 
END IF; 
END=
DELIMITER ;

The errors are:

ERROR 1064 (42000): 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 '*) INTO num_rows FROM autoinc; IF num_rows >' at line 4

 ERROR 1064 (42000): 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 '3 THEN 

Can any please help me fix this?

As already mentioned in the comments:

SELECT (*) AS num_rows ...

was probably meant to be

SELECT COUNT(*) AS num_rows ...

And the

IF num_rows >=3 THEN 

breaks as you defined = as delimiter.

Use a delimiter that doesn't occur in your code, eg:

DELIMITER //

With these two changes things should work without syntax errors

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