之前總是在MSSQL上寫存儲過程,沒有在MYSQL上寫過,也基本沒有用過,今天需要用到MYSQL,研究了下,把項目的需要的存儲過程寫了一部分,寫一下工作總結。這里沒有給出數據庫結構,不討論SQL語句的細節,主要探討存儲過程語法,適合有基礎的人。
先來看段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:“`”這個符號是反單引號,兩個反單引號夾起來的會被當做變量,一般是在定義字段時遇到關鍵字沖突的時候會用到。