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





    人的本事不是与生俱来的,不是你掌握了多少,而是当你面对一个未知问题的时候,你能用多少时间来掌握!
  • 相关阅读:
    ztree : 增删改功能demo与自定义DOM功能demo的结合
    CF786B Legacy 线段树优化建图
    UVA11992 Fast Matrix Operations 一次开多棵线段树
    P3950 部落冲突 树链剖分
    洛谷P1471 方差 线段树维护区间方差
    2019.7.26 T1 树剖+双标记
    P1505 [国家集训队]旅游
    NOIP2015 运输计划 树上差分+树剖
    P1373 小a和uim之大逃离 四维dp,维护差值
    Pyhton之subprocess模块和configparser模块
  • 原文地址:https://www.cnblogs.com/dianshen520/p/4352002.html
Copyright © 2011-2022 走看看