sql循环语句和多分支语句的语法介绍


  本文标签:SQL 循环 语句

  循环操作是数据库中最常见的问题,下面就将为您分别介绍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