MySQL的存储过程与分隔符

Tiger Soldier posted @ 2008年5月04日 07:18 in MySQL , 7165 阅读

在编写MySQL的存储过程的时候,遇到了这么个问题:当存储过程内容有多句,需要用begin...end的时候,只要其中出现了分隔符就会在分隔符的位置报告如下错误:

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 xx

 百思不得其解,查了手册和教程也无头绪。

某天无意中发现,只要把分隔符改为除了“;”以外的,然后在存储过程里用“;”隔开语句就行了,也就是说应该这样写:

delimiter //         #将分隔符改为“//”
CREATE PROCEDURE xxx #创建存储过程
(……)                 #参数
BEGIN                #语句体
  语句1;
  语句2;
  ……
END

回头看MySQL的手册,才发现我理解错了一句话。原文写道:

Use of multiple statements requires that a client is able to send statement strings containing the ; statement delimiter. This is handled in the mysql command-line client with the delimiter command. Changing the ; end-of-statement delimiter (for example, to //) allows ; to be used in a routine body.

意思是说语句体内的语句必须用“;”作为分隔符,但是这会与mysql的命令行分隔符冲突,所以要把命令行分隔符改为其他的。看的时候竟然看囧了……

  • 无匹配
  • 无匹配

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter