How to use While in MYSQL

Asked

Viewed 8,216 times

2

How to use WHILE in Mysql?

I tried with the following expression:

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

And I got the following reply::

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 dia from base_prov_chamada end while' at line 3

My Mysql version is 5.0.

  • You may not have conditions outside the procedure stored in mysql: http://dev.mysql.com/doc/refman/5.0/en/while.html

  • 2

    Welcome to Stack Overflow! If you can explain better the problem you have in particular and, if possible, show code you have done where this problem is. Your question is too broad, see Help Center How to Ask.

2 answers

2

In the above example the Do after condition Do While.

DECLARE c INT DEFAULT 5;

While c > 1 Do

    select dia from base_prov_chamada;

    SET c = c - 1;

End While;

2

I don’t understand what you want to do, but your syntax is wrong. First you need to declare the variable to use, more or less like this:

DECLARE c INT DEFAULT 5;

while c > 1 
    select dia from base_prov_chamada;

    SET c = c - 1;
end while;

This needs to be inside a PROCEDURE, otherwise it doesn’t work.

  • I didn’t know about the trial.

  • Gypsy Thank you very much!

  • @Danielnure Does the answer meet what you need? So please mark the answer as accepted ("V" shaped icon below the score) for the benefit of the community. Thank you!

  • @Gypsy omorrisonmendez Please take a look at discussion on this subject at the goal. It’s okay to ask to accept your answer, but there’s no need to rush it. Thank you.

  • @bfavaretto I read this discussion already, and I agree only in part. There are people who enter and never mark the answer as accepted. And it doesn’t necessarily have to be my answer.

  • Okay, but could you post your opinion on the goal for the community to evaluate? There seems to be a consensus there for now. And the phrase says yes to imply that it’s about your answer.

  • @bfavaretto As expected, the question was not accepted.

  • It happens. AP posted the question and no longer appeared on the site :(

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.