DROP procedure IF EXISTS `proc_carAndCashScore`;
DELIMITER $$
create Procedure proc_carAndCashScore(
in accountName varchar(255),
in pathCode varchar(255),
in indecies text
)
begin
CREATE TEMPORARY TABLE IF NOT EXISTS temp_split
(
col varchar(20)
);
DELETE FROM temp_split;
while(instr(indecies,',')<>0) DO
insert temp_split(col) values (substring(indecies,1,instr(indecies,',')-1)); //截取第一个逗号之前的字符串
set indecies = INSERT(indecies,1,instr(indecies,','),''); //更新字符串,把取到的字符串及逗号换为cut掉,第一次循环后变成”CP0001,CP0002,CP2004“
END WHILE;
insert temp_split(col) values (indecies);//最后一次字符为CP2004,只剩一个字符是没有逗号分割,不能进入循环,在此处插入表中
insert into ac_interface_index (`account_id`,`interface_id`,`index_id`,`created_at`) values (
(select id from account where account =accountName),
(select id from sys_interface where code=pathCode),
(select group_concat(id) from sys_index where code in (select * from temp_split)),
now());
end;$$
调用时执行如下命令:
call proc_carAndCashScore("00000001","queryCarScore","BP1013,CP0001,CP0002,CP2004");
**DELIMITER ∗ ∗ : 告 诉 解 释 器 碰 到 ** : 告诉解释器碰到 ∗∗:告诉解释器碰到代表结束,可以执行命令,默认的的结束符号是";" 但是存储过程中的分号不代表指令结束,因此用这个命令转换一下。
创建存储过程
create procedure proc_name()
begin
.........
end
注意:存储过程名称后面必须加括号,哪怕该存储过程没有参数传递
insert(str,pos,len,newstr)
把字符串str由位置pos起len个字符长的子串替换为字符串
newstr并返回
三.删除存储过程
1.基本语法:
drop procedure sp_name
instr() :返回子字符串在字符串中的位置
SELECT INSTR('hello world', 'l') FROM DUAL; --结果:3
SELECT INSTR('hello world', 'l', 5) FROM DUAL; --结果:10 (从左向右第5位开始检索'l'在'hello world'中出现的位置)
SELECT INSTR('hello world', 'l', -1) FROM DUAL; --结果:10 (从右向左第1位开始检索'l'在'hello world'中出现的位置)
SELECT INSTR('hello world', 'l', 2, 2) FROM DUAL; --结果:4 (从左向右第2位开始检索'l'在'hello world'中第2次出现的位置)
SELECT INSTR('hello world', 'l', -3, 3) FROM DUAL; --结果:0 (从右向左第3位开始检索'l'在'hello world'中第3次出现的位置)