MySql learning experience stored procedure

  • 2020-06-19 11:50:50
  • OfStack

First look at paragraph mysql query article reply statement:


# Query article reply 
-- ----------------------------
-- 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 ;

# Technicalpoint 1: MySql5.1 does not support LIMIT parameters (MySql5.5 does). If you write stored procedures using LIMIT as a variable, you will need to build them with dynamic SQL, which is certainly not as good as static SQL. The main codes are as follows:


         SET @ssra = CONCAT('SELECT * FROM gk_article WHERE id = ? LIMIT ?,?');
         PREPARE sqlquery FROM @ssra;
         EXECUTE sqlquery USING @ra_id,@pagefrom,@pagesize;


# technicpoint 2: If you also want to return the number of rows affected add the statement: ROW_COUNT() function, "; "between the two statements. Space.


# Update the data 
-- ----------------------------
-- 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 ;

Error (Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '=') Convert the comparison equation 1 to CONVERT(ES42en.fullCode USING utf8) COLLATE utf8_unicode_ci). The main code is as follows:


         UPDATE gk_permission SET `level` = @plevel, power = @ppower WHERE user_uid = CONVERT(@puser_uid USING utf8) COLLATE utf8_unicode_ci;


# Insert data 
-- ----------------------------
-- 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;
         # Because of foreign key constraints, the added foreign key fields need to have data in the table where the corresponding foreign key resides 
         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);
         # The query results automatically return the number of rows affected 
END

DELIMITER ;


# According to the ID Delete the data 
-- ----------------------------
-- 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 ;


# Query the user or administrator by account 
-- ----------------------------
-- 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;
         # Query manager 
         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;
         # Query the user 
         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 ;

# technicpoint 4: This counting process requires a control statement (if else elseif loop repeat leave iterate).


         IF (getAdmin = 1) THEN
                   # The statement... 
         END IF;

# Technical point 5: error (Column count doesn't match value count at row 1) in the case of passing in mismatched parameters.

# Technicpoint 6: Function to get the current time: NOW()

The symbol "'" is an inverted comma. Two inverted commas are used as variables. 1.


Related articles: