以下的文章主要讲述的是MySQL 5.0 存储过程编程的基础知识入门,如果你对MySQL 5.0 存储过程编程的相关知识有不懂之处的话,你就可以通过以下的文章对其进行更深入的了解,望你会有所收获。 首先看MySQL 5.0参考手册中关于创建存储过程的语法说明: CREATE [
以下的文章主要讲述的是MySQL 5.0 存储过程编程的基础知识入门,如果你对MySQL 5.0 存储过程编程的相关知识有不懂之处的话,你就可以通过以下的文章对其进行更深入的了解,望你会有所收获。
首先看MySQL 5.0参考手册中关于创建存储过程的语法说明:
<ol class="dp-xml"> <li class="alt"><span><span>CREATE </span></span></li> <li> <span>[</span><span class="attribute">DEFINER</span><span> = { user | CURRENT_USER }] </span> </li> <li class="alt"><span>PROCEDURE sp_name ([proc_parameter[,...]]) </span></li> <li><span>[characteristic ...] routine_body </span></li> <li class="alt"><span>proc_parameter: </span></li> <li><span>[ IN | OUT | INOUT ] param_name type </span></li> <li class="alt"><span>type: </span></li> <li><span>Any valid MySQL data type </span></li> <li class="alt"><span>characteristic: </span></li> <li><span>LANGUAGE SQL </span></li> <li class="alt"><span>| [NOT] DETERMINISTIC </span></li> <li><span>| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA } </span></li> <li class="alt"><span>| SQL SECURITY { DEFINER | INVOKER } </span></li> <li><span>| COMMENT 'string' </span></li> <li class="alt"><span>routine_body: </span></li> <li><span>Valid SQL procedure statement </span></li> </ol>
如果你对MySQL还不太熟悉的话,单单看这个语法结构当然不足以进行MySQL存储过程编程。我之前基本都是使用MS SQL SERVER,所以以下记录我熟悉MySQL存储过程的过程,也是重点介绍MS SQL SERVER与MySQL区别较大的地方。
第一步,当然是写个Hello Word的存储过程,如下:
<ol class="dp-xml"> <li class="alt"><span><span>CREATE PROCEDURE phelloword() </span></span></li> <li><span>BEGIN </span></li> <li class="alt"><span>SELECT 'Hello Word!' AS F; </span></li> <li><span>END; </span></li> </ol>
将上面创建phelloword存储过程的语句拷到phpMyAdmin中执行,报如下错误:
#1064 - 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 3
在这个问题上我纠缠了很久,在MySQL的命令行工具中执行同样不成功,但是根据提示信息可以知道执行在 SELECT 'Hello Word!' AS F;处结束,后面的END;没有执行,这显然会导致错误。
这里MySQL 5.0需要选择以个分隔符,语法如下:DELIMITER //
分隔符是通知MySQL客户端已经输入完成的符号。一直都是用“;”,但是在存储过程中不行,因为存储过程中很多语句都需要用到分号。
因此上面的存储过程改为:
<ol class="dp-xml"> <li class="alt"><span><span>CREATE PROCEDURE ptest() </span></span></li> <li><span>BEGIN </span></li> <li class="alt"><span>SELECT 'Hello Word!' AS F; </span></li> <li><span>END // </span></li> </ol>
另外在phpMyAdmin中执行时,在Delimiter文本框中填写 //,这次存储过程即可创建成功。
第二步,写一个包括参数,变量,变量赋值,条件判断,UPDATE语句,SELECT返回结果集的完整的一个存储过程,如下:
<ol class="dp-xml"> <li class="alt"><span><span>CREATE PROCEDURE plogin </span></span></li> <li><span>( </span></li> <li class="alt"><span>p_username char(15), </span></li> <li><span>p_password char(32), </span></li> <li class="alt"><span>p_ip char(18), </span></li> <li><span>p_logintime datetime </span></li> <li class="alt"><span>) </span></li> <li><span>LABEL_PROC: </span></li> <li class="alt"><span>BEGIN </span></li> <li><span>DECLARE v_uid mediumint(8); </span></li> <li class="alt"><span>DECLARE v_realpassword char(32); </span></li> <li><span>DECLARE v_nickname varchar(30); </span></li> <li class="alt"><span>DECLARE v_oltime smallint(6); </span></li> <li><span>SELECT u.uid, u.password, f.nickname, u.oltime INTO v_uid, v_realpassword, v_nickname, v_oltime </span></li> <li class="alt"> <span>FROM cdb_members u INNER JOIN cdb_memberfields f ON </span><span class="attribute">f.uid</span><span> = u.uid WHERE </span><span class="attribute">u.username</span><span> = </span><span class="attribute-value">p_username</span><span>; </span> </li> <li><span>IF (v_uid IS NULL) THEN </span></li> <li class="alt"><span>SELECT 2 AS ErrorCode; </span></li> <li><span>LEAVE LABEL_PROC; </span></li> <li class="alt"><span>END IF; </span></li> <li> <span>IF (p_password </span><span class="tag"><span class="tag">></span><span> v_realpassword) THEN </span></span> </li> <li class="alt"><span>SELECT 3 AS ErrorCode; </span></li> <li><span>LEAVE LABEL_PROC; </span></li> <li class="alt"><span>END IF; </span></li> <li> <span>UPDATE ipsp_userexpands SET </span><span class="attribute">lastloginip</span><span> = </span><span class="attribute-value">p_ip</span><span>, </span><span class="attribute">lastlogintime</span><span> = </span><span class="attribute-value">p_logintime</span><span> WHERE </span><span class="attribute">uid</span><span> = </span><span class="attribute-value">v_uid</span><span>; </span> </li> <li class="alt"><span>SELECT 0 AS ErrorCode, v_uid AS uid, v_nickname AS nickname, v_oltime AS oltime; </span></li> <li><span>END LABEL_PROC // </span></li> </ol>
首先要说的是给变量赋值的语法,MySQL中使用SELECT u.uid, u.password, f.nickname, u.oltime INTO v_uid, v_realpassword, v_nickname, v_oltime FROM cdb_members u INNER JOIN cdb_memberfields f ON f.uid = u.uid WHERE u.username = p_username;这种方式给变量赋值。
其次是条件判断的语法结构,如下所示:
<ol class="dp-xml"> <li class="alt"><span><span>IF ... THEN </span></span></li> <li><span>...; </span></li> <li class="alt"><span>ELSE </span></li> <li><span>IF ... THEN </span></li> <li class="alt"><span>...; </span></li> <li><span>ELSEIF </span></li> <li class="alt"><span>...; </span></li> <li><span>ELSE </span></li> <li class="alt"><span>...; </span></li> <li><span>END IF; </span></li> <li class="alt"><span>END IF; </span></li> </ol>
最后说说LEAVE 语法的使用。当满足某种条件,不继续执行下面的SQL时,在MS SQL SERVER中使用RETURN语法,在MySQL中我没有找到对应的关键字,但是这里可以利用LEAVE语法来满足要求,在存储过程的BEGIN前定义一个标签,如:“LABEL_PROC:” 然后再需要用到RETURN中断执行的地方执行“LEAVE LABEL_PROC;”即可。
第三步,创建一个执行动态SQL的存储过程。
<ol class="dp-xml"> <li class="alt"><span><span>CREATE PROCEDURE ipsp_getresourcedir </span></span></li> <li><span>( </span></li> <li class="alt"><span>p_hashcode char(40) </span></li> <li><span>) </span></li> <li class="alt"><span>LABEL_PROC: </span></li> <li><span>BEGIN </span></li> <li class="alt"><span>DECLARE v_sql varchar(200); </span></li> <li> <span>SET </span><span class="attribute">v_sql</span><span> = </span><span class="attribute-value">CONCAT</span><span>('SELECT filedir FROM ipsp_resources WHERE </span><span class="attribute">hashcode</span><span> =\'', p_hashcode, '\' LIMIT 0, 1'); </span> </li> <li class="alt"> <span>SET @</span><span class="attribute">sql</span><span> = </span><span class="attribute-value">v_sql</span><span>; </span> </li> <li><span>PREPARE sl FROM @sql; </span></li> <li class="alt"><span>EXECUTE sl; </span></li> <li><span>DEALLOCATE PREPARE sl; </span></li> <li class="alt"><span>END LABEL_PROC // </span></li> </ol>
这里提一下 “\”是转义字符,拼接成的SQL类似 SELECT filedir FROM ipsp_resources WHERE hashcode ='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' LIMIT 0, 1
另外@sql这个属于用户变量,具体用法请查询MySQL参考手册。
如果有在MS SQL SERVER上编写存储过程的经验的话,看完这些,我想基本的MySQL存储过程编程应该可以应付了吧!
想了解更多的内容可查询MySQL参考手册或者相关书籍!以上的相关内容就是对小题大做之MySQL 5.0存储过程编程入门
的介绍,望你能有所收获。