资源描述:
《mysql学习心得之存储过程》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、mysql学习心得之存储过程先来看段mysql查询文章回复语句:代码如下:#查询文章回复procedurestruetureforsp_select_reply_articledropprocedureifexistssp_select_reply_article:delimiter;:createdefiner=rootlocalhostproceduresp_select_reply_article(inra_idint,inpagefromint,inpagesize'int)begin#routine
2、bodygoeshere…setra_id=ra_id;setpagefrom=pagefrom;setpagesize=pagesize;setssra=concat(’select氺fromgk_articlewhereid=?limit?,?’);preparesqlqueryfromssra;executesqlqueryusingra_id,pagefrom,pagesize;enddelimiter;#技术点1:mysql5.1不支持limit参数(mysql5.5就支持了),如果编写存储过程时使
3、用limit做变量,那是需要用动态sQl来构建的,而这样做性能肯定没有静态sql好。主要代码如下:代码如下:setssra=concatCselect氺fromgk_articlewhereid=?limit?,?’);preparesqlqueryfromssra;executesqlqueryusingra_id,pagefrom,pagesize;財支术点2:如果同时需要返回受影响行数需要在语句后面添加语句:row_count()函数,两条语句之间需要“;分隔。代码如下:#更新数据—procedures
4、tructurefor'sp_update_permission'dropprocedureifexistssp_update_permission:delimiter;;createdefiner=rootlocalhostproceduresp_update_permission(inpuser_uid'varchar(20),inplevelint,inppowerint)beginttroutinebodygoeshere…setpuser_uid=puser_uid;setplevel=plevel
5、;setppower=ppower;updategk_permissionset'level=plevel,power=ppowerwhereuser_uid=convert(puser_uidusingutf8)collateutf8_unicode_ci:enddelimiter;#技术点3:mysql进行字符串比较时发生错误(illegalmixofcollations(utf8_unicode_ci,implicit)and(utf8_general_ci,implicit)foroperation’
6、=’),解决方法:将比较等式一边进行字符串转换,如改为“convert(b.fullcodeusingutf8)collateutf8_unicode_ci”,主要代码如下:代码如下:updategk_permissionsetlevel=plevel,power=ppowerwhereuser_uid=convert(puseruidusingutf8)collateutf8Unicodeci;代码如下:#插入数据procedurestructurefor'sp_insert_userdropprocedu
7、reifexistssp_insertuser;delimiter;;createdefiner='root''localhost'procedure'sp_insert_user'(inuidvarchar(20),inupwvarchar(32),innamevarchar(20),insexint,inphonevarchar(20),in'uidint,ins_idint,in'j_id'int)beginttroutinebodygoeshere..•setuid=uid;setupw=upw;se
8、tuname=uname:setsex=sex;setphone=phone;#由于外键约束,所以添加的外键字段需要在对应外键所在表有相应数据setu_id=u_id;setsid=sid;setj_id=j_id;setverifytime=date(’0000-00-00’);insertintogkuser(uid,upw,uname,sex,phone,u_id,s_id,j_id,veri