您的代碼塊沒有爲聲明的變量定義範圍。如果在程序中,它們必須介於BEGIN
和END
之間。沒有他們,陳述DECLARE varLocalityName VARCHAR(50);
成爲無效的聲明將被執行。該語句等效於如下所示的語句:
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
或遵循存儲過程使用範圍變量定義的語法。
例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
。
實施例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>
也可參考Variables declaration and scope。
你想做什麼? '存儲過程'?功能?或者是什麼? –
這只是我試圖執行的一段陳述。它是我寫的存儲過程的一部分。 – Sameer
你可以在存儲過程中添加腳本嗎? –