57,064
社区成员
发帖
与我相关
我的任务
分享
DELIMITER //
create procedure request_info(client_id int, services varchar(20))
begin
declare comma_pos int;
declare current_id int;
svcs: loop
set comma_pos = locate(',', services);
set current_id = substr(services, 1, comma_pos);
if current_id <> 0 then
set services = substr(services, comma_pos+1);
else
set current_id = services;
end if;
insert into service_info values(null, client_id, current_id);
if comma_pos = 0 or current_id = '' then
leave svcs;
end if;
end loop;
end//
//
DELIMITER ;
call request_info("45", "1,4,6");
mysql> delimiter //
mysql> create procedure request_info(client_id int, services varcha
-> begin
-> declare comma_pos int;
-> declare current_id varchar(30);
-> svcs: loop
-> set comma_pos = locate(',', services);
-> set current_id = substr(services, 1, comma_pos);
-> if current_id <> '' then
-> set services = substr(services, comma_pos+1
-> else
-> set current_id = services;
-> end if;
-> insert into service_info values(null, client_id, cu
-> if comma_pos = 0 or current_id = '' then
-> leave svcs;
-> end if;
-> end loop;
-> end//
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> select * from service_info;
+--------+-----------+---------+
| row_id | client_id | service |
+--------+-----------+---------+
| 49 | 45 | 1 |
| 50 | 45 | 4 |
| 51 | 45 | 6 |
| 52 | 45 | 999 |
| 53 | 45 | 999 |
| 54 | 45 | 4 |
+--------+-----------+---------+
6 rows in set (0.00 sec)
mysql> call request_info("45", "1,4,6");
Query OK, 1 row affected (0.08 sec)
mysql> select * from service_info;
+--------+-----------+---------+
| row_id | client_id | service |
+--------+-----------+---------+
| 49 | 45 | 1 |
| 50 | 45 | 4 |
| 51 | 45 | 6 |
| 52 | 45 | 999 |
| 53 | 45 | 999 |
| 54 | 45 | 4 |
| 55 | 45 | 1 |
| 56 | 45 | 4 |
| 57 | 45 | 6 |
+--------+-----------+---------+
9 rows in set (0.00 sec)
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.5.16 |
+-----------+
1 row in set (0.00 sec)
mysql> TRUNCATE `service_info`;
Query OK, 0 rows affected (0.03 sec)
mysql> CALL request_info("45", "1,4,6");
Query OK, 1 row affected (0.06 sec)
mysql> SELECT * FROM `service_info`;
+--------+------------+----------+
| row_id | aclient_id | aservice |
+--------+------------+----------+
| 1 | 45 | 1, |
| 2 | 45 | 4, |
| 3 | 45 | 6 |
+--------+------------+----------+
3 rows in set (0.00 sec)
mysql>
mysql> delimiter //
mysql> create procedure request_info(client_id int, services varcha
-> begin
-> declare comma_pos int;
-> declare current_id varchar(30);
-> svcs: loop
-> set comma_pos = locate(',', services);
-> set current_id = substr(services, 1, comma_pos);
-> if current_id <> '' then
-> set services = substr(services, comma_pos+1
-> else
-> set current_id = services;
-> end if;
-> insert into service_info values(null, client_id, cu
-> if comma_pos = 0 or current_id = '' then
-> leave svcs;
-> end if;
-> end loop;
-> end//
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> select * from service_info;
+--------+-----------+---------+
| row_id | client_id | service |
+--------+-----------+---------+
| 49 | 45 | 1 |
| 50 | 45 | 4 |
| 51 | 45 | 6 |
| 52 | 45 | 999 |
| 53 | 45 | 999 |
| 54 | 45 | 4 |
+--------+-----------+---------+
6 rows in set (0.00 sec)
mysql> call request_info("45", "1,4,6");
Query OK, 1 row affected (0.08 sec)
mysql> select * from service_info;
+--------+-----------+---------+
| row_id | client_id | service |
+--------+-----------+---------+
| 49 | 45 | 1 |
| 50 | 45 | 4 |
| 51 | 45 | 6 |
| 52 | 45 | 999 |
| 53 | 45 | 999 |
| 54 | 45 | 4 |
| 55 | 45 | 1 |
| 56 | 45 | 4 |
| 57 | 45 | 6 |
+--------+-----------+---------+
9 rows in set (0.00 sec)
mysql> create procedure request_info(client_id int, services varchar(20))
-> begin
-> declare comma_pos int;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '' at
line 3
root@localhost : test 03:37:05>create procedure request_info(client_id int, services varchar(20))
-> begin
-> declare comma_pos int;
-> declare current_id varchar(30);
-> svcs: loop
-> set comma_pos = locate(',', services);
-> set current_id = substr(services, 1, comma_pos);
-> if current_id <> '' then
-> set services = substr(services, comma_pos+1);
-> else
-> set current_id = services;
-> end if;
-> insert into service_info values(null, client_id, current_id);
-> if comma_pos = 0 or current_id = '' then
-> leave svcs;
-> end if;
-> end loop;
-> end//
Query OK, 0 rows affected (0.00 sec)
root@localhost : test 03:37:05>DELIMITER ;
root@localhost : test 03:37:52>select * from service_info;
Empty set (0.00 sec)
root@localhost : test 03:37:56>call request_info("45", "1,4,6");
Query OK, 1 row affected (0.00 sec)
root@localhost : test 03:38:40>select * from service_info;
+----+-----------+------------+
| id | client_id | current_id |
+----+-----------+------------+
| 1 | 45 | 1, |
| 2 | 45 | 4, |
| 3 | 45 | 6 |
+----+-----------+------------+
3 rows in set (0.00 sec)
root@localhost : test 03:38:43>call request_info("45", "999");
Query OK, 1 row affected (0.00 sec)
root@localhost : test 03:38:50>select * from service_info;
+----+-----------+------------+
| id | client_id | current_id |
+----+-----------+------------+
| 1 | 45 | 1, |
| 2 | 45 | 4, |
| 3 | 45 | 6 |
| 4 | 45 | 999 |
+----+-----------+------------+
4 rows in set (0.00 sec)