mssql 與 mysql 中While語句使用方法

來源:互聯網
上載者:User

mssql 與 mysql教程 中while語句使用方法

while的使用水平能夠有些協助。

while boolean_expression
    { sql_statement | statement_block }
    [ break ]
    { sql_statement | statement_block }
    [ continue ]

參數
boolean_expression

返回 true 或 false 的運算式。如果布林運算式中含有 select 語句,必須用圓括弧將 select 語句括起來。

{sql_statement | statement_block}

transact-sql 語句或用語句塊定義的語句分組。若要定義語句塊,請使用控制流程關鍵字 begin 和 end。

break

導致從最內層的 while 迴圈中退出。將執行出現在 end 關鍵字後面的任何語句,end 關鍵字為迴圈結束標記。

continue

使 while 迴圈重新開始執行,忽略 continue 關鍵字後的任何語句


文章轉載自網管之家:http://www.bitscn.com/pdb/mssql/201010/191381.html

範例程式碼:
 代碼如下:
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

mysql中,使用while迴圈處理資料方法:需要建立為預存程序,直接調用執行預存程序。

範例程式碼:
 代碼如下:
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,"測試試題","0");
set i = i + 1;
end while;
end;

執行個體二

mysql>
mysql> delimiter $$
mysql>
mysql> create procedure myproc()
    -> 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 an 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();

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.