• SQL server 变量if,while,存储过程


    一.变量

    1.if循环

    2.

    3.while循环

      declare @ss int
      set @ss =2
    while @ss<10
    begin
      print 'Hello'
      set @ss=@ss+1
    end
      --break 跳出循环

      declare @sss int
      set @sss=2
      while @sss<10
    begin
      print'Hello'
      set @sss=@sss+1
    if @sss=6
      break
    end
      --continue 跳出本次循环,继续下次循环

      declare @ssss int
      set @ssss=2
    while @ssss<10
    begin
      set @ssss=@ssss+1
    if @ssss>4and @ssss<7
      continue
      print'Hello'
    end

    4.练习,查看循环的过程

    5.case when --相当于if 但是有局限性,需要具体到值
    --查询老师表的年龄,如果>=35,【老头子】
    --<=30【小青年】
    --其他,【正当年】
    select age,
    case age
    when 35 then '老头子'
    when 36 then '老头子'
    when 37 then '老头子'
    when 38 then '老头子'
    when 30 then '小青年'
    when 29 then '小青年'
    when 28 then '小青年'
    when 27 then '小青年'
    else '正当年'
    end
    from teacher

    6.查询英语分数最高的学生的性别,若男,【这是一个男生】
    --若女,【这是一个小姑娘】
    --若英语分数超过90,【成绩非常好,继续保持】
      declare @xing char(10)
      select @xing =sex from students where code =(select top 1 code from score order by yingfen desc)
      declare @yingfen int
    if @xing ='男'
      print'这是一个男生'
    else if @xing ='女'
      print'这是一个小姑娘'
    else if @yingfen>90
      print'成绩非常好,继续把持'

    7.查询数学分数最高的学生的任课教师的姓名和科目,
    --【**老师教课质量高,是个**教师!】
    declare @jname varchar(50)
    declare @lesson char(10)
    select @jname=name from teacher where code=
    (select shujiao from Student where code=
    (select top 1 code from score order by shufen desc) )

    select @lesson=lesson from teacher where name =@jname
    print @jname +'老师教课质量高'+'是个'+rtrim(@lesson)+'教师'

    7.查询三班语文教师的工号,姓名,以及所教科目
    select code,lesson,name from teacher where code=(select top 1 yujiao from student where banji='三班' order by yujiao)

    8.查询总分最高的学生的语文教师的所有信息
    select * from teacher where code =
    (select yujiao from student where code =
    (select top 1 code from score group by code order by SUM(shufen+yufen+yingfen)desc))

     二.存储

    1.create proc firstproc--创建一个存储过程
    as --存储过程关键字
    select * from student--存储过程的语句
    go
    --执行存储过程的语句(两个都可以)
    exec firstproc
    execute firstproc

    --存储过程可以有返回值
    --定义一个变量去接收
    declare @fanhui int
    exec @fanhui = firstproc--需要执行之后才会有返回值,用变量接收
    select @fanhui as 返回值--查看返回值

    --修改存储过程的语句
    alter proc firstproc
    as
    select * from score
    go

    练习: 利用存储过程查找语文教师张晓华所教课程的学生的分数
            过80的算优秀,优秀人数超过3个人即为【教师评测达标】
            若不到3个人,【不达标】
      create proc thirdproc
    as
      declare @code int
      select @code=code from teacher where name ='张晓华'
      declare @count int
      select @count =COUNT(*)from score where code in (select code from students where yujiao =@code)and yufen>80
    if @count>3
      print'教师评测达标'
    else
      print'教师评测不达标'
    go
      exec thirdproc

    2.

     

    3.

    练习:存储过程
    查看所输入编号的学生是否能够结业,两门以上及格即可结业
    三门都及格,【优秀】
    两门及格,【结业】
    一门及格,【不结业】
    三门都不及格,【请重修】
      create proc sevenproc
      @code int
    as
      declare @yufen decimal(18,2),@shufen decimal(18,2),@yingfen decimal(18,2)
      select @yufen =yufen from score where code =@code--分别查询语数英的分数
      select @shufen =shufen from score where code =@code
      select @yingfen =yingfen from score where code =@code
      declare @count int--定义标记变量
      set @count=0 --标记变量在下面需要先使用再赋值,所以先给它为0
    if @yufen>=60 --判断语数英是否及格
      set @count=@count+1--及格的时候count+1
    if @shufen>=60
      set @count=@count+1
    if @yingfen>=60
      set @count=@count+1

    if @count=3 --判断count的值:判断几门课及格
      print'优秀'
    else if @count=2
      print'结业'
    else if @count=1
      print'不结业'
    else
      print'请重修'
    go
    --执行
      exec sevenproc 4

    4.不带参数带返回值的存储过程
    create proc elevenproc
    as
    return 5
    go
    --执行
    --需要一个变量来接收这个返回值
    declare @fan int
    exec @fan=elevenproc
    print @fan

    5.带参数,带返回值的存储过程
    create proc twelveproc
    @one int,
    @two int
    as
    declare @sum int
    set @sum=@one+@two
    return @sum
    go
    --执行
    declare @fanhuizonghe int
    exec @fanhuizonghe = twelveproc 2,4
    print @fanhuizonghe

    练习:输入一个学生的学号,想要经过存储过程之后得到在这个学生的总分
    create proc thirteenproc
    @code int
    as
      declare @yu decimal(18,2),@shu decimal(18,2),@ying decimal(18,2)
      select @yu =yufen from score where code =@code
      select @shu =shufen from score where code =@code
      select @ying =yingfen from score where code =@code
      declare @sum int
      select @sum=@yu+@shu+@ying
      return @sum
    go

      declare @fan int
      exec @fan =thirteenproc 5
      print @fan

     

  • 相关阅读:
    delphi xe10 FMX 启动参数
    delphi xe6 JSON 测试
    oracle实现http请求,oracle发送http请求。
    ORACLE存储过程调用Web Service
    新搭建的iis服务器,运行网站报 System.BadImageFormatException:未能加载文件或程序集”....“或它的某一个依赖项。
    c#的http请求工具类核心代码
    vue-cli3 取消关闭eslint 校验代码
    quartz.net数据库持久化教程
    sql备份一张表的数据
    iis 长期无访问导致定时任务不执行的解决方案
  • 原文地址:https://www.cnblogs.com/Fate-rail/p/4993110.html
Copyright © 2020-2023  润新知