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 ;
#技巧點1:MySql5.1不支撐LIMIT參數(MySql5.5就支撐了),假如編寫存儲進程時應用LIMIT做變量,那是須要用靜態SQL來構建的,而如許做機能確定沒有靜態SQL好。重要代碼以下:
SET @ssra = CONCAT('SELECT * FROM gk_article WHERE id = ? LIMIT ?,?');
PREPARE sqlquery FROM @ssra;
EXECUTE sqlquery USING @ra_id,@pagefrom,@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 ;
#技巧點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.*, un.`name`, se.`name`, jo.`name` FROM gk_user AS us, gk_unit AS un, gk_section AS se, 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 ;
#技巧點4:這個存數進程須要用到掌握語句(if else elseif while loop repeat leave iterate)。
IF (getAdmin = 1) THEN
#語句…
END IF;
#技巧點5:在傳入參數不婚配的情形下報錯(Column count doesn't match value count at row 1),這個就是仔細成績了,具體檢討參數吧。
#技巧點6:獲得以後時光的函數:NOW()
#技巧點7:“`”這個符號是反單引號,兩個反單引號夾起來的會被當作變量,普通是在界說字段時碰到症結字抵觸的時刻會用到。