Insert multiple records consecutively using the MySQL Stored Procedure

Source: Internet
Author: User

To create a project in these days, you need to send messages to the group. Because the group user id value cannot be determined, you need to use the stored procedure to implement it. However, mysql5.0 does not support arrays. after a long time of study, I finally got it done.

 

Code
1 -- procedure "p_message_multiinsert" DDL
2
3 create procedure 'P _ message_multiinsert '(param_state smallint,
4
5 param_type smallint,
6
7 param_sms smallint,
8
9 param_senderid int,
10
11 param_sendtime datetime,
12
13 param_receivetime datetime,
14
15 param_receiverids varchar (1000 ),
16
17 param_title varchar (60 ),
18
19 param_content varchar (4000 ),
20
21 param_count INT)
22 begin
23
24
25
26 declare I int (8) default 0;
27
28 repeat set I = I + 1;
29
30 Insert t_message
31
32 (
33
34 f_state,
35
36 f_type,
37
38 f_sms,
39
40 f_senderid,
41
42 f_sendtime,
43
44 f_receivetime,
45
46 f_receiverid,
47
48 f_title,
49
50 f_content
51
52)
53
54 values
55
56
57
58 (
59
60 param_state,
61
62 param_type,
63
64 param_sms,
65
66 param_senderid,
67
68 param_sendtime,
69
70 param_receivetime,
71
72 substring_index (param_receiverids, ',', I), ',',-1 ),
73
74 param_title,
75
76 param_content
77
78 );
79
80 until I> = param_count
81
82 end repeat;
83
84end;
85

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.