C#开发学习——存储过程

举个例子:

带输入参数的存储过程
计算班级中英语和数学不及格的人数 

    if(exists(select * from sys.objects where name='usp_GetFailCount'))
        drop proc usp_GetFailCount
        go

    create proc usp_GetFailCount--开始创建存储过程
    @EngPass int,--存储过程的参数不需要加declare
    @MathPass int
    as       
       声明变量存储英语成绩不及格和数学成绩不及格的人数
        declare @engCount int
        declare @mathCount int

        --给人数赋值
        select @engCount=Count(*) from Score where english<@EngPass
        select @mathCount=Count(*) from Score where math<@MathPass

        --输出结果
        select '英语不及格的人数',@engCount
        select '数学不及格的人数',@mathCount

      执行存储过程
        exec usp_GetFailCount
        --传入两个需要的参数
        exec usp_GetFailCount 80,60
        --想让英语和数学的及格分数线都是60
        exec usp_GetFailCount @EngPass=60,@MathPass=60
       


      总结:

存储过程如果没有默认的参数 
传参的方式

1.直接传入跟参数类型一样的值

2.@参数名=值,个数必须跟参数要求的一致

存储过程如果有默认的参数

1.不传,采用的默认值

2.传1个,另一个就是默认值

3.传2个,会把默认值覆盖

 

带输出参数的存储过程

if(exists(select * from sys.objects where name='usp_GetEngFailCount'))
       drop proc usp_GetEngFailCount
    go
    create proc usp_GetEngFailCount
    @EngPass int,--输入参数
    @MathPass int,
    将数学成绩不及格的人数 使用输出参数返回
    @MathFailCount int output--输出参数
    as
      先求英语不及格的人数
 declare @EngFailCount int
      select @EngFailCount=Count(*) from Score where english<@EngPass


      select @MathFailCount=Count(*) from Score where math<@MathPass

      -只打印英语不及格的人数
      select '英语不及格的人数',@EngFailCount

     Declare @mCount int
      --调用
     exec usp_GetEngFailCount 60,60,@mCount output
    输出数学不及格的人数
     select '数学不及格的人数',@mCount

posted @ 2017-01-10 14:42  蒙小米  阅读(674)  评论(0编辑  收藏  举报