【发布时间】:2018-02-14 05:32:30
【问题描述】:
据我所知,游标的唯一用途是从堆栈中为每个 fetch 逐个选取值
在 shell 脚本中,同样可以使用 FOR 或 WHILE 循环来实现。
例子:
while read ip;
do
echo $ip
done < ./test.txt
(OR)
IFS=$'\n'
for ip in $(cat ./test.txt)
do
echo "$ip"
done
这里如果 test.txt 有多行,那么对于一次迭代,一行将被加载到变量 ip。
如何在没有游标和使用 FOR(或)WHILE 循环的 mysql 过程中实现相同的功能。
以下是使用游标的过程示例。我只想在以下过程中应用解决方案。
delimiter $$
create procedure Replace_URL_IP()
begin
declare finished varchar(20);
declare ip varchar(20);
declare c1 cursor for select SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(VALUE,'/',3),'//',-1),':',1) IP from rbt_parameters where PARAM like '%URL%' and VALUE like 'http%';
declare continue handler for NOT FOUND set finished=1;
open c1;
start_loop: loop
fetch c1 into ip;
update rbt_parameters set value=replace(value,ip,'127.0.0.1') where PARAM like '%URL%' and VALUE like 'http%';
if finished=1 then
leave start_loop;
end if;
end loop;
close c1;
end
$$
【问题讨论】:
标签: mysql sql shell stored-procedures scripting