How to use While in MYSQL

2

How to use WHILE in MySQL?

I tried the following expression:

set @C = 5 while @C > 1 select dia from base_prov_chamada end while;

And I got the following answer:

  

You have an error in your SQL syntax; check the manual that   correspondent to Your MySQL server version for the right syntax to use   near 'while @c > 1 select day from base_prov_chamada end while 'at   line 3

My version of MySQL is 5.0.

    
asked by anonymous 05.06.2014 / 18:32

1 answer

2

I do not understand what you want to do, but your syntax is wrong. First you need to declare the variable and then use it, like this:

DECLARE c INT DEFAULT 5;

while c > 1 
    select dia from base_prov_chamada;

    SET c = c - 1;
end while;

This must be within PROCEDURE , otherwise it will not work.

    
05.06.2014 / 18:38