Mysql数据库中 ,涉及事物,循环添加数据
1 create PROCEDURE Usp_AddBoradCast
2 (
3 in subjects VARCHAR(200),
4 in detail text,
5 in useid int,
6 in isdelete int,
7 in confirm INT,
8 in time DateTime,
9 in acceptUserId VARCHAR(500),
10 in isread INT
11
12 )
13 BEGIN
14 declare cnt int default 0;
15 declare i int default 0;
16 declare growthid int DEFAULT 0;
17 DECLARE t_error INTEGER DEFAULT 0;
18 DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1;
19 START TRANSACTION;
20
21 INSERT into wc_broadcast(id,`subject`,content,userId,isDelete,isConfirm,addDate)VALUES(subjects,detail,useid,isdelete,confirm,time);
22 select LAST_INSERT_ID() into growthid;
23 /*字符串切割后的总数*/
24 //&为分隔符
25 //func_get_split_string_tota 是函数
26 set cnt = func_get_split_string_total(acceptUserId,'&');
27 WHILE i<cnt
28 DO
29 insert INTO wc_broadcastinfo(id,broadcastid,userid,isread)VALUES(growthid,func_get_split_string(acceptUserId,'&',i),isread);
30 set i = i + 1;
31 end WHILE;
32
33 IF t_error = 0 THEN
34 commit;
35 ELSE
36 ROLLBACK;
37 END IF;
38
39 END
2 (
3 in subjects VARCHAR(200),
4 in detail text,
5 in useid int,
6 in isdelete int,
7 in confirm INT,
8 in time DateTime,
9 in acceptUserId VARCHAR(500),
10 in isread INT
11
12 )
13 BEGIN
14 declare cnt int default 0;
15 declare i int default 0;
16 declare growthid int DEFAULT 0;
17 DECLARE t_error INTEGER DEFAULT 0;
18 DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1;
19 START TRANSACTION;
20
21 INSERT into wc_broadcast(id,`subject`,content,userId,isDelete,isConfirm,addDate)VALUES(subjects,detail,useid,isdelete,confirm,time);
22 select LAST_INSERT_ID() into growthid;
23 /*字符串切割后的总数*/
24 //&为分隔符
25 //func_get_split_string_tota 是函数
26 set cnt = func_get_split_string_total(acceptUserId,'&');
27 WHILE i<cnt
28 DO
29 insert INTO wc_broadcastinfo(id,broadcastid,userid,isread)VALUES(growthid,func_get_split_string(acceptUserId,'&',i),isread);
30 set i = i + 1;
31 end WHILE;
32
33 IF t_error = 0 THEN
34 commit;
35 ELSE
36 ROLLBACK;
37 END IF;
38
39 END