操作環境:有表game_list,字段:uid,score1,score2,seat_id,last_update;
傳入參數為i_player_detail ,傳入的值為多個用戶的id、之前分數、之后分數、座位號,每個用戶的數據用分號(;)隔開;
操作目的:將各個用戶對應的屬性插入到目標表對應的字段中,last_update為數據更新日期;
傳入參數i_player_detail ,里面存放多個用戶的信息,每個用戶的一組數據用分號隔開,每個用戶的信息多個,比如
“用戶id,score,desk,seat;
用戶id,score,desk,seat;……”
-- 使用存儲過程delimiter $$use `log_pdk`$$drop procedure if exists `game_c`$$create procedure `game_c` (in i_player_detail varchar(500))SQL SECURITY INVOKERBEGINDROP TABLE IF EXISTS `temp_list`;--創建臨時表,將截取的數據先插入到臨時表CREATE TEMPORARY TABLE `temp_list`(`uid` INT(10) UNSIGNED NOT NULL,`score1` INT(10) UNSIGNED NOT NULL,`score2` INT(10) UNSIGNED NOT NULL,`seat_id` TINYINT(3) UNSIGNED NOT NULL);-- declare str varchar(500);-- 用來拼接sql動態語句declare m_detail varchar(500);declare m_num tinyint;-- 當傳入的用戶信息字符串中含有分號';',進行截取set m_num = position(';' in str) -- 不存在分號的時候,返回0while m_num >= 1 dobeginset @str = 'insert into temp_list values (' + substring(m_detail,1,m_num-1)+')' -- 截取第一個用戶的信息(第一個分號前面的字符),插入到臨時表prepare statement1 from @str;execute statement1;deallocate prepare statement1;set m_detail = substring(m_detail,m_num+1); -- 定義除去第一個用戶和分號那部分的字符串set set m_num = position(';' in str);end while;-- 從臨時表抽出所有字段,添加時間字段,插入到表game_listINSERT INTO `game_list`(`uid`,`score1`,`score2`,`seat_id`, `last_update`)SELECT `uid`, `score1`, `score2`, `seat_id`, current_date() FROM `temp_list`;end$$delimiter ;
新聞熱點
疑難解答