mysql看不出来语法错误在哪,无法在MySQL语法错误中运行查询

I'm running Workbench 5.2.47.

I have a long procedure I wrote with basic data checking. If a record did not exist in the database, the record would be inserted.

The procedure saved with no problems, but MySQL 5.5 throws an error when I try running it.

It is long, and has a lot of company sensitive data in it, or I would post it here.

I am trying to debug the procedure by executing small chunks of the code, but I can't seem to get Workbench to allow anything I try.

MySQL shows how to create a stored procedure in 5.1.5 Working with Stored Procedures.

Let me show you something very basic I am trying to write:

DELIMITER $$

DROP PROCEDURE IF EXISTS my_test;

CREATE PROCEDURE my_test()

BEGIN

SELECT * FROM Employees;

END $$

DELIMITER ;

With that, Workbench gives me the error, "syntax error, unexpected CREATE, expecting $end".

ef042d59c2f075dcb57e577d615aef17.png

I don't understand that, but I need to get something done, so I am moving on.

I make a simpler query:

SET @Count=(SELECT Count(*) FROM tbl_object_users WHERE username='jp2code');

IF (@Count < 1) THEN

INSERT INTO tbl_object_users (username, date_time) VALUES ('jp2code', NOW());

END IF;

Again, I get an error, this time on my IF statement.

92b6d1c0ab139975358701efb1270603.png

Next, I go into PhpMyAdmin to try running something from there using its database:

SET @Count=Count(id) FROM `tbl_object_users` WHERE `username`='jp2code';

It, too, tells me I have an error in my SQL syntax.

25231b8af5e6dcde2778d14befa05fe8.png

I did download and install the newest Workbench 6, but it did not solve the problem - and I did not like the interface, so I uninstalled it and went back to Workbench 5.2.

What is going on? SQL isn't that hard, so what is with these hurdles?

解决方案

Problem with this:

DELIMITER $$

DROP PROCEDURE IF EXISTS my_test;

CREATE PROCEDURE my_test() ...

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.

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.

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.

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

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

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 is expecting an expression after the equals sign. MySQL is not expecting a SQL statement.

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'

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值