且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法在MySQL语法错误中运行查询意外

更新时间:2023-02-26 09:50:50

问题:

DELIMITER $$
DROP PROCEDURE IF EXISTS my_test;
CREATE PROCEDURE my_test() ...

是MySQL在DROP PROCEDURE语句行的末尾看不到分号作为语句的末尾.这是因为前一行告诉MySQL,语句终止符不是分号.您告诉MySQL,语句将以两个美元符号终止.因此,MySQL正在读取DROP PROCEDURE行,以查找语句终止符.而且读取的整个Blob都不是有效的MySQL语句,它会产生语法错误.

is that MySQL isn't seeing the semicolon at the end of the DROP PROCEDURE statement line as the end of the statement. This is because the preceding line told MySQL that the statement terminator was something other than a semicolon. You told MySQL that statements were going to be terminated with two dollar signs. So MySQL is reading the DROP PROCEDURE line, looking for the statement terminator. And the whole blob it reads is NOT a valid MySQL statement, it generates a syntax error.

解决方法:要么将DROP PROCEDURE行移至 前,要么移至DELIMITER $$行;或使用指定的定界符而不是分号终止DROP PROCEDURE语句.

The fix: either move the DROP PROCEDURE line before the DELIMITER $$ line; or terminate the DROP PROCEDURE statement with the specified delimiter rather than a semicolon.

您报告的第二个问题是语法错误.发生这种情况是因为MySQL无法将 IF 识别为有效SQL语句的开头.

The second problem you report is a syntax error. That's occurring because MySQL doesn't recognize IF as the beginning of a valid SQL statement.

IF语句在MySQL存储程序的上下文中(例如,在CREATE PROCEDURE语句中)仅有效.

The IF statement is valid only within the context of a MySQL stored program (for example, within a CREATE PROCEDURE statement.)

解决方法:仅在MySQL存储程序的上下文中使用IF语句.

The fix: Use an IF statement only within the context of a MySQL stored program.

您报告的第三个问题也是语法错误.发生这种情况是因为您没有SET语句的有效语法. SET语句为用户变量分配值的MySQL语法是:

The third problem you report is also a syntax error. That's occurring because you don't have a valid syntax for a SET statement; MySQL syntax for SET statement to assign a value to user variable is:

SET @uservar = expr 

MySQL期望等号后出现表达式. MySQL 需要SQL语句.

MySQL is expecting an expression after the equals sign. MySQL is not expecting a SQL statement.

要将值作为SELECT语句的结果分配给用户变量,请在SELECT语句中进行分配,例如:

To assign a value to a user variable as the result from a SELECT statement, do the assignment within the SELECT statement, for example:

SELECT @Count := Count(id) FROM `tbl_object_users` WHERE `username`='jp2code'

请注意,SELECT语句中的赋值运算符为 := (等于冒号),而不仅仅是 = .

Note that the assignment operator inside the SELECT statement is := (colon equals), not just =.