操作環(huán)境:有表game_list,字段:uid,score1,score2,seat_id,last_update;
傳入?yún)?shù)為i_player_detail ,傳入的值為多個用戶的id、之前分數(shù)、之后分數(shù)、座位號,每個用戶的數(shù)據(jù)用分號(;)隔開;
操作目的:將各個用戶對應的屬性插入到目標表對應的字段中,last_update為數(shù)據(jù)更新日期;
傳入?yún)?shù)i_player_detail ,里面存放多個用戶的信息,每個用戶的一組數(shù)據(jù)用分號隔開,每個用戶的信息多個,比如
“用戶id,score,desk,seat;
用戶id,score,desk,seat;……”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
-- 使用存儲過程 delimiter $$ use `log_pdk`$$ drop procedure if exists `game_c`$$ create procedure `game_c` ( in i_player_detail varchar (500)) SQL SECURITY INVOKER BEGIN DROP TABLE IF EXISTS `temp_list`; --創(chuàng)建臨時表,將截取的數(shù)據(jù)先插入到臨時表 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動態(tài)語句 declare m_detail varchar (500); declare m_num tinyint; -- 當傳入的用戶信息字符串中含有分號';',進行截取 set m_num = position( ';' in str) -- 不存在分號的時候,返回0 while m_num >= 1 do begin set @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_list INSERT INTO `game_list`(`uid`,`score1`,`score2`,`seat_id`, `last_update`) SELECT `uid`, `score1`, `score2`, `seat_id`, current_date () FROM `temp_list`; end $$ delimiter ; |