Use of While statements in MSSQL and MySQL

Source: Internet
Author: User
Tags closing tag mssql mysql tutorial

The While statement usage method in the MSSQL and MySQL tutorial

While the level of use can help a little.

While Boolean_expression
{sql_statement | statement_block}
[Break]
{sql_statement | statement_block}
[Continue]

Parameters
Boolean_expression

Returns an expression of True or false. If a Boolean expression contains a SELECT statement, you must enclose the SELECT statement in parentheses.

{sql_statement | statement_block}

Transact-SQL statements or groups of statements defined with statement blocks. To define a statement block, use the control flow keyword begin and end.

Break

Causes the exit from the inner while loop. Any statements that appear after the end keyword will be executed, and the ending keyword is a circular closing tag.

Continue

Causes the while loop to restart execution, ignoring any statements after the Continue keyword


Article reprinted from the Home network: http://www.bitscn.com/pdb/mssql/201010/191381.html

Sample code:
The code is as follows:
DECLARE @a int
Set @a = 1
While @a<25
Begin
Insert into demotable (ID,ITEM1,ITEM2) VALUES (@a, "abc", "123")
Set @a = @a + 1
End

In MySQL, use a while loop to process data methods: You need to create a new stored procedure to call the Execute stored procedure directly.

Sample code:
The code is as follows:
Create definer= ' root ' @ ' localhost ' procedure ' newprocedure ' ()
Begin
declare i int;
Set I=1;
While i<100 do
Insert into demotable (ID,ITEM1,ITEM2) VALUES (i, "Test questions", "0");
Set i = i + 1;
End while;
End

Example Two

Mysql>
mysql> delimiter $$
Mysql>
Mysql> CREATE PROCEDURE MyProc ()
   -& Gt Begin
   
   ->     declare i int;
    ->     set I=1;
   ->     loop1:while i<=10 do
   ->           if mod (i,2) <>0 then/*even number-try again*/
    ->             Select concat (i, "is a odd Number ");
   ->          End If;
   ->          set i=i+1;
   ->     End While Loop1;
   -> end$$
Query OK, 0 rows Affected (0.00 sec)

Mysql>
Mysql> delimiter;
Mysql> call MyProc ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.