WEB开发网
开发学院数据库MSSQL Server SQL server入门:T-SQL编程 阅读

SQL server入门:T-SQL编程

 2008-09-04 10:00:38 来源:WEB开发网   
核心提示: >[3] if-else 条件语句if(条件) begin 语句1 语句2 …… endelse ……declare @myavg floatset @myavg = avg(writtenexam) from stumarks

>[3] if-else 条件语句

  if(条件)
    begin
     语句1
     语句2
     ……
    end
  else
     ……
  
  declare @myavg float
  set @myavg = avg(writtenexam) from stumarks
  print 平均分+convert(varchar(5),@myavg)
  if(@myavg>70)
  begin
   print 本班笔试成绩优秀,前三名的成绩为:
   select top 3 * form stumarks order by writtenexam desc
    end
  else
    begin
     print 本班笔试成绩较差,后三名的成绩为:
     select top 3 * from stumarks order by writtenexam 
    end  

……………………………………………………………………………………………………………………………………

>[4] while 循环语句

  declare @n int
  while(1=1)-----条件永远成立
     begin
        set @n = count(*) from stumarks where writtenexam<60
      if(@n>0)
        update stumarks set writtenexam = writtenexam+2
      else
        break
     end

print 加分后的成绩为:

select * from stumarks

……………………………………………………………………………………………………………………………………

>[5] case 多分支语句

  case
     when 条件1 then 结果1
    when 条件2 then 结果2
     ………
    else
  end
  
  select * form stumarks
  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

上一页  1 2 

Tags:SQL server 入门

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接