zoukankan      html  css  js  c++  java
  • SQL里面的IF ELSE,没有{},使用begin...end

    --IF ELSE
    --特点:
    --1.没有{},使用begin...end
    --2.有多重和嵌套
    --3.if必须有处理语句,如果只有一句,也可以不使用begin..end,只包含后面一句
    --4.没有true/false的概念,只能使用关系运算符的条件表达式
    --5.if后面的括号可以没有

    --计算office平均分数并输出,如果平均分数超过60分输出成绩最高的三个学生的成绩,否则输出后三名的学生
    go
    declare @subjectname nvarchar(50)='office' --科目名称
    declare @subjectId int=(select subjectId
    from subject where subjectname=@subjectname) --获取科目ID
    declare @avg int --科目平均分
    set @avg=(select AVG(StudentResult) from Result
    where SubjectId=@subjectId and StudentResult is not null) --获取指定科目的平均分
    --做判断
    if (@avg>=95)
    begin
    print '成绩不错。输出前三名'
    select top 3 * from Result where SubjectId=@subjectId
    and StudentResult is not null order by StudentResult desc
    end
    else
    begin
    print '成绩不好。输出后三名'
    select top 3 * from Result where SubjectId=@subjectId
    and StudentResult is not null order by StudentResult
    end
    -------------
    go
    declare @name nvarchar(50)='office'--科目名称
    declare @id int =(select SubjectId from Subject where SubjectName =@name )--科目ID
    declare @avg int = (select AVG (StudentResult ) from Result
    where SubjectId =@id and StudentResult is not null )--获取指定科目的平均分
    --做判断
    if(@avg >=95)
    begin
    print '成绩不错,输出前三名'
    select top 3*from Result where SubjectId =@id and StudentResult is not null
    order by StudentResult desc
    end
    else
    begin
    print '成绩不好,输出后三名'
    select top 3 *from Result where SubjectId =@id and StudentResult is not null
    order by StudentResult asc
    end





    人的本事不是与生俱来的,不是你掌握了多少,而是当你面对一个未知问题的时候,你能用多少时间来掌握!
  • 相关阅读:
    并发基础(一) 线程介绍
    java基础(九) 可变参数列表介绍
    全球 43 亿 IPv4 地址已耗尽!IPv6,刻不容缓
    IPv6,无需操作就可升级?
    为什么 HTTPS 比 HTTP 安全
    从《国产凌凌漆》看到《头号玩家》,你就能全面了解5G
    再谈 APISIX 高性能实践
    API 网关的选型和持续集成
    尹吉峰:使用 OpenResty 搭建高性能 Web 应用
    鱼和熊掌可兼得?一文看懂又拍云 SCDN
  • 原文地址:https://www.cnblogs.com/dianshen520/p/4352002.html
Copyright © 2011-2022 走看看