我的Sql代码是这样的:
DELIMITER $$
CREATE FUNCTION `get_theme`(`section_type` VARCHAR(50))
RETURNS VARCHAR(50)
CONTAINS SQL
SQL SECURITY DEFINER
BEGIN
DECLARE name,dirname VARCHAR(50) ;
SELECT a.name INTO name , a.dirname INTO dirname FROM themes as a
INNER JOIN sections as b ON
a.section_id = b.id
WHERE b.name = 'administrators' AND a.`status` = 'Y';
RETURN name + '#' + dirname;
END $$
DELIMITER ;
当我在heidiSQL 9.2.0.4961中执行此代码时,我收到此错误:未定义变量:a
但是当我执行这段代码时:
SELECT a.name as name , a.dirname as dirname FROM themes as a
INNER JOIN sections as b ON
a.section_id = b.id
WHERE b.name = 'administrators' AND a.`status` = 'Y';
没有问题,它完全正常。
我的代码出了什么问题?任何想法?
答案 0 :(得分:1)
您使用的语法错误。
正确的语法是:
SELECT list_of_expressions INTO list_of_variables FROM ...
替换此片段:
SELECT a.name INTO name , a.dirname INTO dirname FROM themes as a
这一个:
SELECT a.name, a.dirname INTO name , dirname FROM themes as a
一句话:不要分配与表中列相同的变量,例如:DECLARE name,dirname
,如果要在查询中稍后使用它们,则会导致混淆和错误。为变量使用前缀,例如:DECLARE v_name,v_dirname