Question

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;
#

I'm looping through a series of arithmetic sequences. This code should either add or subtract, depending on the first what the first two parameters are. Then we check if the step value is positive or negative. I'm getting an error that says to check the syntax at:

set v_start = v_start + v_step ; 
        select v_start as 'The Loop Output';
        set v_step = v_step - 1;

So then I deleted the two when statements that contain that and ran my code through. There were no errors. But now I'm not sure what to do next. I don't see what needs to be corrected. Any suggestions? Thank you.

Was it helpful?

Solution

Your error is in the while v_start > v_stop then right above the snippet you posted. It should read while v_start > v_stop do instead. (Actually, you have it in a couple of places; check every while statement.)

See the MySQL documentation here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top