sql迴圈語句和多分支語句的文法介紹

來源:互聯網
上載者:User

迴圈操作是資料庫中最常見的問題,下面就將為您分別介紹sql迴圈語句和多分支語句的文法,希望對您學習sql的迴圈語句能夠有些許協助。

一 迴圈文法

while (條件--只能是運算式)

語句或語句塊

[break]--強制退出

二 例子:

declare @n int

while(1=1)

begin

select @n=count(*) from stuMarks where writtenExam<60--統計不及格人數

if(@n>0)

update stuMarks set writtenExam=writtenExam+2--每人加兩分

else

break--退出迴圈

end

三 多分支語句文法

case

when 條件1 then 結果1

when 條件2 then 結果2

[else 其他結果]

end

四   例子:

select stuNo,成績=case

when writtenExam<60 then 'e'

when writtenExam between 60 and 69 then 'd'

when writtenExam between 70 and 79 then 'c'

when writtenExam between 80 and 89 then 'b'

else 'A'

end

from stuMarks

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.