Create procedure count_loop( p_start int,
p_stop int,
p_step int unsigned,
p_delim varchar(5))
begin
declare v_start int ;
declare v_stop int ;
declare v_step int ;
declare v_sign int;
declare v_sum int;
-- check p_start
case p_start
when null then
set v_start := 1;
else
set v_start := p_start;
end case;
-- check p_stop
case p_stop
when null then
set v_stop := 10;
else
set v_stop := p_stop;
end case;
-- check p_stop
case p_step
when null then
set v_step := 1;
when 0 then
set v_step := 1;
else
set v_step := p_step;
end case;
-- set v_sign as v_stop - v_start
set v_sign := (v_stop - v_start) ;
case
-- if v_sign and v_step are negative,
-- then run while loop
when v_sign < 0 and v_step < 0 then
while v_start > v_stop then
set v_start = v_start + v_step ;
select v_start as 'The Loop Output';
set v_step = v_step - 1;
end while;
-- if both v_sign and v_step are positive
-- then run loop
when v_sign > 0 and v_step > 0 then
while v_start > v_stop then
set v_start := v_start + v_step ;
select v_start as 'The Loop Output';
set v_step := v_step + 1;
end while;
-- if v_sign and v_step are different signs
-- terminate loop
when v_sign > 0 and v_step < 0 then
select 'Loop collapsed' as 'The Loop Output' ;
when v_sign < 0 and v_step > 0 then
select 'Loop collapsed' as 'The Loop Output' ;
end case ;
end;
#
我正在循环一系列算术序列。此代码应加或减,具体取决于前两个参数是什么。然后我们检查步长值是正还是负。我收到一个错误,说要检查以下位置的语法:
set v_start = v_start + v_step ;
select v_start as 'The Loop Output';
set v_step = v_step - 1;
因此,我删除了包含该内容的两个 when 语句并运行了我的代码。没有错误。但现在我不确定下一步该做什么。我看不出有什么需要纠正的。有什么建议么?谢谢你。