【问题标题】:mysql stored function parameter syntax errormysql存储函数参数语法错误
【发布时间】:2013-08-30 13:46:48
【问题描述】:

这个语法有什么问题?

mysql 5.5、phpmyadmin 3.4

delimiter ;;
create procedure foo(a text, b int, c text)
begin
select * from table_a where attribute1 like %a% 
and attribute2 = b
and attribute3 like %c%
end
;;

phpmyadmin 在第 1 行告诉我语法错误,但无论如何它似乎都不起作用。

【问题讨论】:

  • @AndaIancu 同样的错误,但双分号以前有效,我不认为这是问题

标签: mysql sql database phpmyadmin


【解决方案1】:

在您的情况下,您甚至不需要更改 DELIMITER 并使用 BEGIN ... END 块。您的程序可能看起来像

CREATE PROCEDURE foo(a TEXT, b INT, c TEXT)
SELECT * 
  FROM table_a 
 WHERE attribute1 LIKE CONCAT('%', a, '%')
   AND attribute2 = b
   AND attribute3 LIKE CONCAT('%', c, '%');

这里是SQLFiddle演示

【讨论】:

  • 此答案生成 LIKE 子句,并将参数传递给过程,生成一个字符串。你原来的 %a% 不是一个有效的字符串。
【解决方案2】:

缺少引号。

delimiter ;;

create procedure foo(a text, b int, c text)
begin
  select * from table_a where attribute1 like concat('%', a,' %') 
  and attribute2 = b
  and attribute3 like concat('%', c,' %');
end;;

【讨论】:

  • 这是否意味着我正在寻找类似于字母“a”和“c”的文本?我想使用参数。
猜你喜欢
  • 1970-01-01
  • 2018-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-12
  • 2014-07-12
相关资源
最近更新 更多