• 存储过程 if 复习 while 学习


    --变量
    --全局变量(系统变量)
    --打印上次的语句影响了多少行数据
    print @@rowcount
    --查询自从本次启动SQL开始,连接过几次数据库
    print @@connections

    --局部变量(声明出来,自己赋值,
    --声明的时候可以像C#里面声明的方式declare @a int,@b int
    --set 赋值的时候,只能一个一个赋值)
    --查编号为10的学生的性别,若是男【这是个男生】
    --若是女,【这是个女生】
    declare @sex char(10)--因为需要拿着性别去判断使用,所以需要声明一个变量来接收
    select @sex =sex from student where code =10--在查询语句中可以赋值使用
    if @sex ='男'--判断语句
    begin--begin end 相当于C#中的{ }
    print '这是个男生'
    end
    else
    begin
    print '这是个女生'
    end


    --查询编号为1005的教师的年龄
    --若大于等于45【再干几年就退休了】
    --若小于30【继续好好干】
    --其他【正当年!!】
    declare @age int
    declare @code int
    set @code =1005 --变量的赋值方法
    select @age = age from teacher where code =@code
    if @age>=45 --if else if else 多选一(等同于C#里面的语句)
    print'再干几年就退休了'--若语句为多句,需要加上begin end
    else if @age<30
    print '继续好好干'
    else
    print'正当年!!'

    --运算符的优先级
    --第一级:* / %
    --第二级:正好负号(+)(-)
    --第三级:+ -
    --第四级:> < >= <= != !> !<
    --第五级:not
    --第六级:and or between and
    --第七级:all any in like some exists
    --第八级:=(等号)

    --循环语句
    --while
    declare @ss int
    set @ss =2
    while @ss<10
    begin
    print 'Hello'
    set @ss = @ss+1 --每次更改局部变量的值需要写set
    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>4 and @ssss<7
    continue
    print 'Hello'
    end


    --练习,查看循环的过程
    declare @math int
    set @math =80
    while @math <95
    begin
    print '考的很好!优秀!'+ cast(@math as char(10))
    set @math = @math+1--上面打印分数的时候还是小1的,到了下面判断的时候已经加了1
    if @math = 93
    break
    if @math>85 and @math <=90
    continue
    print 'Hello'
    end

    select *from teacher
    --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

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

    select * from teacher

    --查询数学分数最高的学生的任课教师的姓名和科目,
    --【**老师教课质量高,是个**教师!】
    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)+'教师'

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

    --查询总分最高的学生的语文教师的所有信息
    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))

    --存储过程
    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

    --利用存储过程查找三个表内的信息
    create proc secondproc
    as
    select * from student
    select * from teacher
    select * from score
    go

    --执行
    exec secondproc

    --利用存储过程查找语文教师张晓华所教课程的学生的分数,
    --过80的算优秀,优秀人数超过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 student where yujiao = @code)
    and yufen>80
    if @count >3
    print '教师评测达标'
    else
    print '教师评测不达标'
    go

    exec thirdproc


    --带一个参数的存储过程
    create proc fourthproc
    @one char(10) --as前面写上参数的名称,类型
    as
    print @one--存储过程中就可以使用上面的变量
    go
    --执行
    exec fourthproc '你好啊' --执行时需要将此参数传给存储过程

    --带两个参数的存储过程
    create proc fifthproc
    @two varchar(50), --两个参数或者多个参数时中间用,隔开
    @three varchar(50)
    as
    print @two + @three
    go
    --执行
    exec fifthproc '你好啊!','你在干嘛?'--两个参数用逗号隔开


    --查询学号为我们输入的号码的学生的数学成绩
    create proc sixproc
    @one int
    as
    select shufen from score where code=@one
    go
    --执行
    exec sixproc 8


    --练习:存储过程
    --查看所输入编号的学生是否能够结业,两门以上及格即可结业
    --三门都及格,【优秀】
    --两门及格,【结业】
    --一门及格,【不结业】
    --三门都不及格,【请重修】
    create proc eighthproc
    @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 @count int--定义标记变量
    set @count=0 --标记变量在下面需要先使用再赋值,所以先给他为0
    if @yu>=60 --判断语数英是否及格
    set @count=@count+1--及格的时候count+1
    if @shu>=60
    set @count=@count+1
    if @ying>=60
    set @count=@count+1

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

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


    --带参数,带返回值的存储过程
    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 oneproc
    @code int
    as
    declare @yu int
    select @yu=yufen from score where code=@code
    declare @shu int
    select @shu=shufen from score where code=@code
    declare @ying int
    select @ying=yingfen from score where code=@code
    declare @sum int
    select @sum=@yu+@shu+@ying
    return @sum
    go
    declare @fan int
    exec @fan = oneproc 5
    print @fan

    --在创建存储过程时,我们可以设置它有一个默认值。
    create proc twoproc
    @sum int =10 --设置默认值
    as
    set @sum =@sum +10
    return @sum
    go
    --执行
    declare @sumfan int
    exec @sumfan= twoproc --可以不写默认值的参数,或者写上default 缺省,默认
    print @sumfan


    --存储过程练习:输入一个数,求1~n的和
    create proc threeproc
    @shu int
    as
    declare @i int,@sum int
    set @i=0
    set @sum=0
    while @i<=@shu
    begin
    set @sum=@sum+@i
    set @i=@i+1
    end
    return @sum
    go
    declare @he int
    exec @he=threeproc 10
    print @he


    --存储过程练习:输入一个数求这个1!+2!+...+n!的阶乘
    create proc cproc
    @n int
    as
    declare @i int,@jie int,@sum int
    set @i=1
    set @jie=1
    set @sum=0
    while @i<=@n
    begin
    set @jie*=@i
    set @sum+=@jie
    set @i=@i+1
    end
    return @sum
    go

    declare @jiefan int
    exec @jiefan=cproc 3
    print @jiefan

  • 相关阅读:
    MDK(keil)4.7中文注释乱码解决
    小型功率放大器的设计与制作
    增强输出的电路
    晶体管电路设计学习笔记(一)
    MOSFET学习
    sysTick系统定时器
    C#面向对象 什么是面向对象
    JS基础 超链接、数列的用法,行内元素和块级元素
    JS基础 常用函数、事件、阻止事件冒泡
    JS基础 定时器【setTimeout、setInterval、clearInterval 】
  • 原文地址:https://www.cnblogs.com/yuyingming/p/4993652.html
Copyright © 2020-2023  润新知