0
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.

user1682055
  • 93
  • 2
  • 3
  • 9
  • "I'm getting an error" is meaningless without information about what the error is that you're getting. Please edit your question to provide that information, including the **exact** error message you're seeing. We can't read your mind or see your screen from here. – Ken White Sep 23 '12 at 05:08
  • I edited that part of the comment out, as I saw I read the snippet wrong. :-) The error information is still needed, though - strange you missed that part of my comment. ;-) – Ken White Sep 23 '12 at 05:14
  • I edited in the post itself... I got an error stating to check for the right syntax near that snip-it. – user1682055 Sep 23 '12 at 05:16
  • The entire, **exact** error text was "check the syntax at ..." with no other information? – Ken White Sep 23 '12 at 05:18
  • Yep. I think so: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 'then set v_start = v_start + v_step ; select v_start as 'The Loop Output'' at line 46 – user1682055 Sep 23 '12 at 05:20
  • You're RIGHT! It should be "do". Thank you. Stupid mistake. Thanks again. – user1682055 Sep 23 '12 at 05:28

1 Answers1

0

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

Ken White
  • 123,280
  • 14
  • 225
  • 444