1、IF-ELSE 邏輯判斷語句
語法:IF? (條件)
??????????????? BEGIN?
??????????????????? 語句1躲株,語句2片部,……語句n
????????????????END
???????????ELSE
??????????????? BEGIN
??????????????????? 語句1,語句2霜定,……語句n
??????????????? END
2档悠、循環(huán)語句——WHILE
語法:
????????????WHILE (條件)
??????????????? BEGIN
??????????????????? 語句1,語句2望浩,……語句n
??????????????? END
例如:求1-10之間所有整數(shù)之和:
DECLARE? @i?? int
DECLARE? @sum? int
SET? @i=i
SET? @sum=0
WHILE? (@i<=10)
?????????BEGIN
??????????????SET? @sum=@sum+@i
??????????????SET? @i=@i+1
?????????END
3辖所、分支邏輯控制語句(CASE-END)
語法
CASE
??????? WHEN??? 條件1??? THEN??? 結(jié)果1
??????? WHEN??? 條件2??? THEN??? 結(jié)果2
??????? WHEN??? 條件n??? THEN??? 結(jié)果n
ELSE??? 其他結(jié)果。
例如:有一張Result表
請(qǐng)使用美國等級(jí)標(biāo)準(zhǔn)(A-E)顯示成績(jī)
SELECT? *? (
?????? CASE
????????????? WHEN? StudentResult >= 90 then ‘A’
????????????? WHEN? StudentResult >= 80 then ‘A’
????????????? WHEN? StudentResult >= 70 then ‘A’
????????????? WHEN? StudentResult >= 60 then ‘A’
????????????? ELSE ‘E’
)? AS? 等級(jí)
FROM Result;? (上圖表格為查詢后顯示)