zoukankan      html  css  js  c++  java
  • SQL Server

    Procedure

    ----------------------存储过程相关-------------------------------
    
    ----------------------创建存储过程-------------------------------
    -------------------------方法一----------------------------------
    --或者在简写成,
    --create proc usp_TwoNumSum
    create procedure usp_TwoNumSum
    @num1 int,
    @num2 int
    as 
    begin
       print @num1 + @num2
    end
    --调用1
    exec usp_TwoNumSum 10,30
    --调用2
    declare @n1 int = 10, @n2 int = 98
    exec usp_TwoNumSum @num1= @n1, @num2 = @n2
    -------------------------方法二----------------------------------
    create proc usp_TwoNumSub
    @num1 int,
    @num2 int,
    @result int output --此变量用于返回执行结果
    as
    begin
        set @result = @num1 - @num2 
    end 
    --调用
    declare @s int
    exec usp_TwoNumSub 6, 2, @s output
    select @s  
    ----------------------创建存储过程结束-----------------------------
    --案例一☆☆☆☆☆☆
    --创建一个过程,当用户输入姓名、科目,返回其成绩
    create proc usp_GetEnglishScoreByName
    @name varchar(10),         --姓名
    @catogory nvarchar(10),     --科目
    @englishScore int output --英语分数
    as
    begin
        declare @count int = 0;    --用来存储此人有无该科目的记录
        set @count = (select count(*) from Student t, Scores tt where t.StdUserName = tt.Name and tt.Catogory = @catogory and t.StdUserName = @name);
        --判断一下
        if(@count > 0)
        begin
            --有记录时,返回该科目对应的分数
            set @englishScore= (select tt.Score from Student t, Scores tt where t.StdUserName = tt.Name and tt.Catogory = @catogory and t.StdUserName = @name);
        end
        else
        begin
            --无记录时,返回-1
            set @englishScore = -1;    
        end;
    end;
    --调用过程
    declare @score int = 0
    exec usp_GetEnglishScoreByName 'Jeremy Wu','English',@score output
    select @score
    --删除过程
    drop proc usp_GetEnglishScoreByName
    
    --案例二☆☆☆☆☆☆
    --创建一个过程
    --1.需要3个参数
    --参数1:及格分数线:60
    --参数2:每次提分的分数:2
    --参数3:输出循环提分的次数 output参数
    --2.编写循环提分的存储过程,要求提分后,不及格的人数小于总人数的一半
    --3.分数表ScoreTest
    select * from ScoreTest
    --创建过程
    create proc usp_GetRiseScoreTimes
    @limitScoreLine int,    --及格分数线
    @perScore int,            --每次提分的分数
    @times int =0 output    --循环次数
    as
    begin
        --总人数
        declare @allCount int = 0;
        --不及格的人数
        declare @lowCount int = 0;
        --获取总人数
        set @allCount=(select count(*) from ScoreTest t);
        --获取不及格的人数
        set @lowCount=(select count(*) from ScoreTest t where t.score < @limitScoreLine);
        --不及格的人数小于总人数的一半
        while(@lowCount > (@allCount/2))
        begin
            --给小于及格线的进行提分操作
            update ScoreTest set score = score + @perScore where score < @limitScoreLine;
            --更新提分次数
            set @times = @times + 1;
            --获取不及格的人数
            set @lowCount=(select count(*) from ScoreTest t where t.score < @limitScoreLine);
        end;
        --打印提分的次数
        print @times
    end;
    
    --调用过程
    declare @times int = 0
    exec usp_GetRiseScoreTimes 50, 2, @times output
    --删除表
    drop proc usp_GetRiseScoreTimes

      作者:Jeremy.Wu
      出处:https://www.cnblogs.com/jeremywucnblog/
      本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    DOM性能小记
    利用tween.js算法生成缓动效果
    小游戏(锅打灰太狼)
    DOM应用实例(寻找房祖名)
    学习总结——DOM
    图片预加载
    删除src值为空的img标签
    2019-08-17 纪中NOIP模拟B组
    [SCOI2015] 小凸玩矩阵
    [JZOJ4899] 雪之国度
  • 原文地址:https://www.cnblogs.com/jeremywucnblog/p/12467935.html
Copyright © 2011-2022 走看看