我正在尝试创建一个函数,该函数接收与公交车关联的代码作为参数,并返回公交车在 route 的停靠站数。
我的代码如下:
create function num_paragem(bus_code int)
returns int
Begin
Declare x int default 0;
select count(code_stop) into x from Bus_Stops where cod_bus = code;
end;
我不断收到这些错误:
语法错误,意外的 END_OF_INPUT,需要 ';'
-> 它发生在我声明变量 x 时。
syntax error, unexpected END
-> 它出现在代码的最后一行。
谁能帮我找出函数定义中的错误?
感谢您的帮助。
最佳答案
您遇到了定界符问题:我们需要告诉解析器函数内部的定界符以及函数定义的定界符。
试试这个;
-- set special delimiter
DELIMITER $$
create function num_paragem(bus_code int)
returns int
Begin
Declare x int default 0; -- delimiter inside the function
select count(code_stop) into x from Bus_Stops where cod_bus = code;
end;
-- delimiter of the function definition
$$
-- set delimiter back to normal
DELIMITER ;
关于mysql - 为什么mysql在创建函数时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23554856/