本文介绍了MySQL 语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在 MySQL 中运行简单的脚本.我已将脚本减少到只有一行.

I am unable to run a simple script in MySQL. I have reduced the script to just one line.

DELIMITER $$
DECLARE varLocalityName VARCHAR(50);
$$
DELIMITER ;

错误是:

第 2 行的 ERROR 1064 (42000):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在第 1 行的DECLARE varLocalityName VARCHAR(50)"附近使用的正确语法

$ mysql --version
mysql  Ver 14.14 Distrib 5.1.63, for debian-linux-gnu (x86_64) using readline 6.2

推荐答案

您的代码块没有定义声明变量的范围.如果在一个过程中,它们必须在 BEGINEND 之间.如果没有它们,语句 DECLARE varLocalityName VARCHAR(50); 将成为要执行的无效语句.此语句等效于如下所示的语句:

Your code block does not define the scope for the declared variables. If within a procedure, they must be between BEGIN and END. Without them, the statement DECLARE varLocalityName VARCHAR(50); becomes an invalid statement to be executed. This statement is equivalent to the statements shown below:

mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2012-10-22     |
+----------------+
1 row in set (0.00 sec)

mysql> declare varLocalityName varchar(50);
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 'declare varLocalityName varchar(50)' at line 1
mysql>

您应该声明没有关键字DECLARE的会话变量,或者遵循为存储过程定义的语法来使用作用域变量.

Either you should declare session variables without keyword DECLARE or follow a syntax defined for a stored procedure to use scoped variables.

示例 1:使用会话变量:

mysql> set @x = null;
Query OK, 0 rows affected (0.00 sec)
mysql> select @x;
+------+
| @x   |
+------+
| NULL |
+------+
1 row in set (0.00 sec)

mysql> delimiter $$
mysql> select current_date() into @x;
    -> $$
Query OK, 1 row affected (0.02 sec)

mysql> select @x;
    -> $$
+------------+
| @x         |
+------------+
| 2012-10-22 |
+------------+
1 row in set (0.00 sec)

请注意,您可以在过程中设置/定义会话变量,但不能DECLARE.

Note that you can set/define session variables within a procedure but not DECLARE.

示例 2:使用过程范围变量:

mysql>
mysql> delimiter $$
mysql> create procedure some_x()
    ->    begin
    ->      declare varLocalityName varchar(50);
    ->
    ->      set @sessionDate = null;
    ->      select @sessionDate;
    ->      set @sessionDate = current_date();
    ->      select @sessionDate;
    ->
    ->      select varLocalityName;
    ->    end;
    -> $$
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql>
mysql> call some_x();
+--------------+
| @sessionDate |
+--------------+
| NULL         |
+--------------+
1 row in set (0.00 sec)

+--------------+
| @sessionDate |
+--------------+
| 2012-10-22   |
+--------------+
1 row in set (0.00 sec)

+-----------------+
| varLocalityName |
+-----------------+
| NULL            |
+-----------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> select @sessionDate;
+--------------+
| @sessionDate |
+--------------+
| 2012-10-22   |
+--------------+
1 row in set (0.00 sec)

mysql> select varLocalityName;
ERROR 1054 (42S22): Unknown column 'varLocalityName' in 'field list'
mysql>

另请参阅变量声明和作用域.

这篇关于MySQL 语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 02:37