I am trying to use a declare [variable]
statement within a CREATE FUNCTION
statement.
我试图在CREATE FUNCTION语句中使用declare [variable]语句。
My code is
我的代码是
/* DELIMITER // */
CREATE FUNCTION hello_world()
RETURNS TEXT
DECLARE bae int;
BEGIN
RETURN 'Hello World';
END;
//
/* DELIMITER ; */
The code just worked fine without using declare, but with the declare it gives me the error:
代码只是工作正常而不使用声明,但声明它给了我错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use...
您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以获得正确的语法...
How can I actually use the declare statement inside function or stored procedure?
我怎样才能在函数或存储过程中实际使用declare语句?
2 个解决方案
#1
Don't comment out the delimiter, and declares need to be immediately after the begin statement:
不要注释掉分隔符,并声明需要在begin语句之后立即:
DELIMITER //
CREATE FUNCTION hello_world() RETURNS TEXT
BEGIN
DECLARE bae INT;
RETURN 'Hello World';
END
//
DELIMITER; --restore delimiter
SELECT hello_world() --Hello World
EDIT:
I've never used sql fiddle. I've played with it for 5 minutes and I hate it :)
我从来没用过sql小提琴。我玩了5分钟,我讨厌它:)
Apparantly, sql fiddle has different syntax rules. The one that applies here is that they do not support delimiters, but do have //
built in as a "known delimiter" (Execute triggers stored procedures on SqlFiddle. Mysql)
显然,sql小提琴有不同的语法规则。这里适用的是它们不支持分隔符,但确实//内置为“已知分隔符”(执行触发SqlFiddle上的存储过程.Mysql)
So, this works:
所以,这有效:
CREATE function hello_world() returns text
BEGIN
declare bae int;
return "hello world";
END//
select hello_world()
http://sqlfiddle.com/#!2/8e5da4/1
So, there is a working fiddle. I hand-typed the function and it works. HOWEVER, I literally copy pasted that into a new sqlfiddle and I get
所以,有一个工作小提琴。我手动输入功能,它的工作原理。但是,我真的把它粘贴到一个新的sqlfiddle中,然后我就得到了
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 '' at line 3
您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第3行的“'附近使用正确的语法
So this inconsistant behavior is pretty annoying.
所以这种不一致的行为非常烦人。
#2
In mysql, you preface each declaration with declare
, inside the begin
-end
block:
在mysql中,您在开始结束块内为每个声明添加了声明:
CREATE FUNCTION hello_world()
RETURNS TEXT
BEGIN
DECLARE bae int;
RETURN 'Hello World';
END;
#1
Don't comment out the delimiter, and declares need to be immediately after the begin statement:
不要注释掉分隔符,并声明需要在begin语句之后立即:
DELIMITER //
CREATE FUNCTION hello_world() RETURNS TEXT
BEGIN
DECLARE bae INT;
RETURN 'Hello World';
END
//
DELIMITER; --restore delimiter
SELECT hello_world() --Hello World
EDIT:
I've never used sql fiddle. I've played with it for 5 minutes and I hate it :)
我从来没用过sql小提琴。我玩了5分钟,我讨厌它:)
Apparantly, sql fiddle has different syntax rules. The one that applies here is that they do not support delimiters, but do have //
built in as a "known delimiter" (Execute triggers stored procedures on SqlFiddle. Mysql)
显然,sql小提琴有不同的语法规则。这里适用的是它们不支持分隔符,但确实//内置为“已知分隔符”(执行触发SqlFiddle上的存储过程.Mysql)
So, this works:
所以,这有效:
CREATE function hello_world() returns text
BEGIN
declare bae int;
return "hello world";
END//
select hello_world()
http://sqlfiddle.com/#!2/8e5da4/1
So, there is a working fiddle. I hand-typed the function and it works. HOWEVER, I literally copy pasted that into a new sqlfiddle and I get
所以,有一个工作小提琴。我手动输入功能,它的工作原理。但是,我真的把它粘贴到一个新的sqlfiddle中,然后我就得到了
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 '' at line 3
您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第3行的“'附近使用正确的语法
So this inconsistant behavior is pretty annoying.
所以这种不一致的行为非常烦人。
#2
In mysql, you preface each declaration with declare
, inside the begin
-end
block:
在mysql中,您在开始结束块内为每个声明添加了声明:
CREATE FUNCTION hello_world()
RETURNS TEXT
BEGIN
DECLARE bae int;
RETURN 'Hello World';
END;