调用过程时IF-ELSE不工作

时间:2014-07-09 08:33:36

标签: mysql stored-procedures if-statement

我正在尝试使用MySQL命令作为参数调用过程。一切都工作得很好,但最后一个程序调用已经实现了条件语句的行为很糟糕。调用过程时遇到的错误描述如下:

错误代码:1064。您的SQL语法出错;查看与您的MySQL服务器版本对应的手册,以便在'IF @CheckExists>附近使用正确的语法。 0那么                     设置@ result =('完成')

有关更多信息,这里我传递的语句本应在程序中实现(我猜,循环,游标条件必须在MySQL中的过程中定义),因此出错。但我无法弄清楚这一点,因为数据库名称(在这种情况下是@dbname)必须动态传递,即使我试图制作另一个程序来使这项工作我最终得到同样的错误。

非常感谢任何帮助。

由于

use test;

drop procedure if exists test;
set @dbname = 'test_qa';-- only for the sake of example. this is to be dynamically done                     


delimiter //
create procedure test(IN sqlCommand text) 
begin

 SET @tquery = sqlCommand;
 PREPARE stmt FROM @tquery;
 EXECUTE stmt;
DEALLOCATE PREPARE stmt;

end//

delimiter ;

CALL test (concat('SET @CheckExists =(select COUNT(*) from ',@dbname,'.course 
                        where Name = ''Computing'' and Value = ''True'')'));

-- call test ('select @CheckExists');

CALL test (concat('IF @CheckExists > 0 THEN
                set @result = (''done'') 
                else 
                set @result = (''gone'')
                end if'));

1 个答案:

答案 0 :(得分:0)

你不能在MySQL中的匿名块中使用IF(或程序代码)。

使用此:

CALL test('SET @result = CASE WHEN @checkExists > 0 THEN ''done'' ELSE ''gone'' END')