《MysqL实例MysqL学习心得之存储过程》要点:
本文介绍了MysqL实例MysqL学习心得之存储过程,希望对您有用。如果有疑问,可以联系我们。
MysqL教程先来看段mySQL查询文章回复语句:
代码如下:#查询文章回复
-- ----------------------------
-- Procedure structure for `sp_select_reply_article`
-- ----------------------------
DROP PROCEDURE IF EXISTS `sp_select_reply_article`;
DEliMITER ;;
CREATE defineR=`root`@`localhost` PROCEDURE `sp_select_reply_article`(IN `ra_ID` int,IN `pagefrom` int,IN `pagesize` int)
BEGIN
#Routine body goes here...
SET @ra_ID = ra_ID;
SET @pagefrom = pagefrom;
SET @pagesize = pagesize;
SET @ssra = CONCAT('SELECT * FROM gk_article WHERE ID = ? liMIT ?,?');
PREPARE sqlquery FROM @ssra;
EXECUTE sqlquery USING @ra_ID,@pagefrom,@pagesize;
END
DEliMITER ;
MysqL教程#技术点1:MysqL5.1不支持liMIT参数(MysqL5.5就支持了),如果编写存储过程时使用liMIT做变量,那是必要用动态sql来构建的,而这样做性能肯定没有静态sql好.主要代码如下:
代码如下:SET @ssra = CONCAT('SELECT * FROM gk_article WHERE ID = ? liMIT ?,@pagesize;
#技术点2:如果同时需要返回受影响行数需要在语句后面添加语句:ROW_COUNT()函数,两条语句之间需要“;”分隔. 代码如下:
#更新数据
-- ----------------------------
-- Procedure structure for `sp_update_permission`
-- ----------------------------
DROP PROCEDURE IF EXISTS `sp_update_permission`;
DEliMITER ;;
CREATE defineR=`root`@`localhost` PROCEDURE `sp_update_permission`(IN `puser_uID` varchar(20),IN `plevel` int,IN `ppower` int)
BEGIN
#Routine body goes here...
SET @puser_uID = puser_uID;
SET @plevel = plevel;
SET @ppower = ppower;
UPDATE gk_permission SET `level` = @plevel,power = @ppower WHERE user_uID = CONVERT(@puser_uID USING utf8) ColLATE utf8_unicode_ci;
END
DEliMITER ;
MysqL教程#技术点3:MysqL进行字符串比较时发生错误(Illegal mix of collations (utf8_unicode_ci,IMPliCIT) and (utf8_general_ci,IMPliCIT) for operation '='),解决办法:将比较等式一边进行字符串转换,如改为“CONVERT(b.fullCode USING utf8) ColLATE utf8_unicode_ci”,主要代码如下:
代码如下:UPDATE gk_permission SET `level` = @plevel,power = @ppower WHERE user_uID = CONVERT(@puser_uID USING utf8) ColLATE utf8_unicode_ci;
代码如下:
#插入数据
-- ----------------------------
-- Procedure structure for `sp_insert_user`
-- ----------------------------
DROP PROCEDURE IF EXISTS `sp_insert_user`;
DEliMITER ;;
CREATE defineR=`root`@`localhost` PROCEDURE `sp_insert_user`(IN `uID` varchar(20),IN `upw` varchar(32),IN `name` varchar(20),IN `sex` int,IN `phone` varchar(20),IN `u_ID` int,IN `s_ID` int,IN `j_ID` int)
BEGIN
#Routine body goes here...
SET @uID = uID;
SET @upw = upw;
SET @uname = uname;
SET @sex = sex;
SET @phone = phone;
#由于外键约束,所以添加的外键字段必要在对应外键所在表有相应数据
SET @u_ID = u_ID;
SET @s_ID = s_ID;
SET @j_ID = j_ID;
SET @verifytime = DATE('0000-00-00');
INSERT INTO gk_user(uID,upw,uname,sex,phone,u_ID,s_ID,j_ID,verifytime)
VALUES(@uID,@upw,@uname,@sex,@phone,@u_ID,@s_ID,@j_ID,@verifytime);
#查询结果会自动返回受影响行数
END
DEliMITER ;
代码如下:
#根据ID删除数据
-- ----------------------------
-- Procedure structure for `sp_delete_exchange_by_ID`
-- ----------------------------
DROP PROCEDURE IF EXISTS `sp_delete_exchange_by_ID`;
DEliMITER ;;
CREATE defineR=`root`@`localhost` PROCEDURE `sp_delete_exchange_by_ID`(IN `eID` int)
BEGIN
#Routine body goes here...
SET @eID = eID;
DELETE FROM gk_exchange WHERE ID = @eID;
END
DEliMITER ;
代码如下:
#通过账号查询用户或者管理员
-- ----------------------------
-- Procedure structure for `sp_select_user_by_uID`
-- ----------------------------
DROP PROCEDURE IF EXISTS `sp_select_user_by_uID`;
DEliMITER ;;
CREATE defineR=`root`@`localhost` PROCEDURE `sp_select_user_by_uID`(IN `uID` varchar(20),IN `getadmin` int)
BEGIN
#Routine body goes here...
SET @uID = uID;
#SET @getadmin = getadmin;
#查询管理员
IF (getadmin = 1) THEN
SELECT us.*,un.`name`,se.`name`,jo.`name`,pe.`level`,pe.power FROM gk_user AS us,gk_unit AS un,gk_section AS se,gk_jobTitle AS jo,gk_permission AS pe WHERE us.u_ID = un.ID AND us.s_ID = se.ID AND us.j_ID = jo.ID AND us.uID = pe.user_uID AND us.uID = CONVERT(@uID USING utf8) ColLATE utf8_unicode_ci;
END IF;
#查询用户
IF (getadmin = 0) THEN
SELECT us.*,jo.`name` FROM gk_user AS us,gk_jobTitle AS jo WHERE us.u_ID = un.ID AND us.s_ID = se.ID AND us.j_ID = jo.ID AND us.uID = CONVERT(@uID USING utf8) ColLATE utf8_unicode_ci;
END IF;
END
DEliMITER ;
MysqL教程#技术点4:这个存数过程必要用到控制语句(if else elseif while loop repeat leave iterate).
代码如下:IF (getadmin = 1) THEN
#语句…
END IF;
MysqL教程#技术点5:在传入参数不匹配的情况下报错(Column count doesn't match value count at row 1),这个便是细心问题了,详细检查参数吧.
MysqL教程#技术点6:获取当前时间的函数:Now()
MysqL教程#技术点7:“`”这个符号是反单引号,两个反单引号夹起来的会被当做变量,一般是在界说字段时遇到关键字冲突的时候会用到.
《MysqL实例MysqL学习心得之存储过程》是否对您有启发,欢迎查看更多与《MysqL实例MysqL学习心得之存储过程》相关教程,学精学透。内存溢出PHP学院为您提供精彩教程。
总结以上是内存溢出为你收集整理的Mysql实例MySql学习心得之存储过程全部内容,希望文章能够帮你解决Mysql实例MySql学习心得之存储过程所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)