以下的文章主要讲述的是mysql 5.0 存储过程编程的基础知识入门,如果你对mysql 5.0 存储过程编程的相关知识有不懂之处的话,你就可以通过以下的文章对其进行更深入的了解,望你会有所收获。
首先看mysql 5.0参考手册中关于创建存储过程的语法说明:
create [definer = { user | current_user }] procedure sp_name ([proc_parameter[,...]]) [characteristic ...] routine_body proc_parameter: [ in | out | inout ] param_name type type: any valid mysql data type characteristic: language sql | [not] deterministic | { contains sql | no sql | reads sql data | modifies sql data } | sql security { definer | invoker } | comment 'string' routine_body: valid sql procedure statement
如果你对mysql还不太熟悉的话,单单看这个语法结构当然不足以进行mysql存储过程编程。我之前基本都是使用ms sql server,所以以下记录我熟悉mysql存储过程的过程,也是重点介绍ms sql server与mysql区别较大的地方。
第一步,当然是写个hello word的存储过程,如下:
create procedure phelloword() begin select 'hello word!' as f; end;
将上面创建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客户端已经输入完成的符号。一直都是用“;”,但是在存储过程中不行,因为存储过程中很多语句都需要用到分号。
因此上面的存储过程改为:
create procedure ptest() begin select 'hello word!' as f; end //
另外在phpmyadmin中执行时,在delimiter文本框中填写 //,这次存储过程即可创建成功。
第二步,写一个包括参数,变量,变量赋值,条件判断,update语句,select返回结果集的完整的一个存储过程,如下:
create procedure plogin ( p_username char(15), p_password char(32), p_ip char(18), p_logintime datetime ) label_proc: begin declare v_uid mediumint(8); declare v_realpassword char(32); declare v_nickname varchar(30); declare v_oltime smallint(6); 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; if (v_uid is null) then select 2 as errorcode; leave label_proc; end if; if (p_password > v_realpassword) then select 3 as errorcode; leave label_proc; end if; update ipsp_userexpands set lastloginip = p_ip, lastlogintime = p_logintime where uid = v_uid; select 0 as errorcode, v_uid as uid, v_nickname as nickname, v_oltime as oltime; end label_proc //
首先要说的是给变量赋值的语法,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;这种方式给变量赋值。
其次是条件判断的语法结构,如下所示:
if ... then ...; else if ... then ...; elseif ...; else ...; end if; end if;
最后说说leave 语法的使用。当满足某种条件,不继续执行下面的sql时,在ms sql server中使用return语法,在mysql中我没有找到对应的关键字,但是这里可以利用leave语法来满足要求,在存储过程的begin前定义一个标签,如:“label_proc:” 然后再需要用到return中断执行的地方执行“leave label_proc;”即可。
第三步,创建一个执行动态sql的存储过程。
create procedure ipsp_getresourcedir ( p_hashcode char(40) ) label_proc: begin declare v_sql varchar(200); set v_sql = concat('select filedir from ipsp_resources where hashcode =\'', p_hashcode, '\' limit 0, 1'); set @sql = v_sql; prepare sl from @sql; execute sl; deallocate prepare sl; end label_proc //
这里提一下 “\”是转义字符,拼接成的sql类似 select filedir from ipsp_resources where hashcode ='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' limit 0, 1
另外@sql这个属于用户变量,具体用法请查询mysql参考手册。
如果有在ms sql server上编写存储过程的经验的话,看完这些,我想基本的mysql存储过程编程应该可以应付了吧!
想了解更多的内容可查询mysql参考手册或者相关书籍!以上的相关内容就是对小题大做之mysql 5.0存储过程编程入门
的介绍,望你能有所收获。