Home mysql MySQL: Why does Whele not working or any other cycle

MySQL: Why does Whele not working or any other cycle

Author

Date

Category

decided with MSSQL to study MySQL on a little bit and came across a problem immediately …

Question: Why is it not working?

set @counter = 1;
While (@counter & lt; 20) do
  SET @Counter = @Counter + 1;
End While;

SQL version is: MySQL-Installer-Community-5.11.0.

compiled this thing in Eclipse, as well as in the console using: Delimiter //

how much did not google, how many different options for other cycles did not try, everything is a stupid, says the syntactic error.


This is:

set @counter = @counter + 1;

And this is:

set @counter = 1;

Compile separately normally, but with the While-Ohm … = (


Begin after DO has already tried to put. I also tried to put a name with a colon and then close this thing in End. It does not work. Repeat also does not work …


In the procedure, by the way, also does not work:

create procedure mypro ()
Begin.
Set @counter = 1;
While (@counter & lt; 20) do
  SET @Counter = @Counter + 1;
End While;
End.

Where am I stupid?


Answer 1, Authority 100%

While Only inside Begin End works. In general, this code works for sure:

create procedure procedure1 ()
Begin.
set @p: = 2;
While @ P & LT; 20 do set @p: = @ p ​​+ 1; End While;
end;

Answer 2, Authority 25%

In fact, the answer from above (@ vkovalchuk88) exhaustive, all logical operators, such as (if statement, Switch Case, While Do, Loop …)
I don’t work in the lads or procedures, see how to write functions and procedures in SQL, there, in addition, everything, except foreach, I did not find it, it is written that it is introduced to Musk from 2011, but my MySQL does not see it.

https://www.w3schools.com/sql/sql_ref_sqlserver.asp

https: // docs .microsoft.com / EN-US / SQL / T-SQL / Statements / Create-Function-Transact-SQL

https: // docs .microsoft.com / EN-US / SQL / T-SQL / Statements / Create-Procedure-transact-SQL

https://www.w3resource.com/sql/sql-procedure.php

The only one kind of function returns the result, and the procedure seems to be no, this is your member.


Regarding your procedure, lacking the delimetra, and the sign “//”, at the beginning and at the end, as described in the code.
concat () and @result , just to demonstrate his work.

deleimiter // - Is ImpoRant
  CREATE PROCEDURE MYPRO ()
  Begin.
  Set @counter = 1;
  Set @result = "";
  While (@counter & lt; 20) do
    SET @Counter = @Counter + 1;
    Set @result = concat (@Result, ",", @counter);
  End While;
  SELECT @RESULT;
END // - End Need Be WITH "//"

now works

Previous articleImplement pascal stack
Next articleDelphi SendMessage

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions