zoukankan      html  css  js  c++  java
  • while 循环,存储过程

    1、while 循环

    declare @ss int
    set @ss=2
    while @ss<10
    begin
    set @ss=@ss+1
    print 'HELLO'+convert(char(10),@ss)
    if @ss=7
    break
    end


    declare @sss int
    set @sss=2
    while @sss<10
    begin
    set @sss=@sss+1
    if @sss=7
    continue
    print 'HELLO'+convert(char(10),@sss)
    end

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

    2、存储过程

    ①    没有参数,没有返回值

    --利用存储过程查找语文教师张晓华所教课程的学生的分数,
    --过80的算优秀,优秀人数超过3个人即为【教师评测达标】
    --若不到三个人,【不达标】
    create proc x
    as
    declare @count decimal(18,2)
    select @count=COUNT(*) from score where fcode
    in(select xcode from student where yujiao=(select tcode from teacher where name='邓凯'))and yufen>80
    if @count>=3
    print '教师测评达标'
    else
    print '不达标'
    go
    exec x--执行

    ② 有参数 ,没有返回值

    --查看所输入编号的学生是否能够结业,两门以上及格即可结业
    --三门都及格,【优秀】
    --两门及格,【结业】
    --一门及格,【不结业】
    --三门都不及格,【请重修】
    alter proc xinproc
    @shu int
    as
    declare @shufen decimal(18,2),@yufen decimal(18,2),@yingfen decimal(18,2)
    select @shufen=shufen,@yufen=yufen,@yingfen=yingfen from score where fcode=@shu
    declare @sum int
    set @sum=0
    if @shufen>=60
    set @sum+=1
    if @yufen>=60
    set @sum+=1
    if @yingfen>=60
    set @sum+=1
    if @sum=3
    print '优秀'
    if @sum=2
    print '及格'
    if @sum=1
    print '不及格'
    if @sum=0
    print '重修'
    go
    exec xinproc 2

    ③  有一个参数,有返回值

    --输入一个学生的学号,想要经过存储过程之后得到在这个学生的总分

    create proc firstproc1
    @shu int
    as
    declare @sum decimal(18,2)
    select @sum=SUM(shufen+yufen+yingfen)from score where fcode=@shu
    return @sum
    go
    declare @fan decimal(18,2)
    exec @fan=firstproc1 2 --定义一个变量接收
    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

    实例:

    --存储过程练习:输入一个数,求1~n的和

    alter proc sec
    @n int
    as
    declare @sum int,@i int
    set @sum=0
    set @i=1
    while @i<=@n
    begin
    set @sum=@sum+@i
    set @i=@i+1
    end
    return @sum
    go
    declare @fan int
    exec @fan=sec 4
    print @fan

    --存储过程练习:输入一个数求这个1!+2!+...+n!的阶乘

    create proc m
    @n int
    as
    declare @sum int,@jie int,@i int
    set @sum=0
    set @jie=1
    set @i=1
    while @i<=@n
    begin
    set @jie=@jie*@i
    set @sum=@sum+@jie
    set @i=@i+1
    end
    return @sum
    go
    declare @fan int
    exec @fan=m 3
    print @fan

  • 相关阅读:
    Python获取秒级时间戳与毫秒级时间戳
    时间戳与时间类型转化(秒级时间戳)
    linux压缩和解压缩命令
    对于Python中@property的理解和使用
    探索性测试方法
    Linux 中 grep 命令的 12 个实践例子
    在 Linux 启动或重启时执行命令与脚本
    亲测的orabbix监控Oracle过程
    find 使用搜集
    Centos7.3-mysql5.7复制安装过程
  • 原文地址:https://www.cnblogs.com/liujianshe1990-/p/4992796.html
Copyright © 2011-2022 走看看