zoukankan      html  css  js  c++  java
  • 存储过程的创建与使用

    1.基本的存储过程

    (1).查询所有学员的信息
    if exists(select * from sysobjects where name='usp_getAllstudent')
    drop proc usp_getAllstudent
    go
    create procedure usp_getAllstudent--创建存储过程
    as
    select * from student
    go
    --调用存储过程
    exec usp_getAllstudent
    2.带参数的存储过程
    (1).查询指定性别的学员信息
    if exists(select * from sysobjects where name='usp_getAllstudentBySex')
    drop proc usp_getAllstudentBySex
    go
    create procedure usp_getAllstudentBySex
    @sex char(2)--形参只是声明,不是定义,所以不需要declare
    as
    select * from student where sex = @sex
    go
    --调用存储过程
    exec usp_getAllstudentBySex '男'

    (2).查询指定性别和班级名称的学员信息
    if exists(select * from sysobjects where name='usp_getAllstudentByClassName')
    drop proc usp_getAllstudentByClassName
    go
    create procedure usp_getAllstudentByClassName
    @sex char(2),
    @className nvarchar(50)
    as
    declare @ClassId int --科目的ID
    set @ClassId = (select classid from grade where classname =@className )
    select * from student where sex = @sex and ClassId=@ClassId
    go
    --调用存储过程,返回指定班级名称和性别信息
    exec usp_getAllstudentByClassName '男','一班'

    3.创建有默认值的存储过程

    (1).查询男性别和班级名称的学员信息
    if exists(select * from sysobjects where name='usp_getAllstudentByClassName')
    drop proc usp_getAllstudentByClassName
    go
    create procedure usp_getAllstudentByClassName
    @sex char(2)='男',
    @className nvarchar(50)
    as
    declare @ClassId int --科目的ID
    set @ClassId = (select classid from grade where classname =@className )
    select * from student where sex = @sex and ClassId=@ClassId
    go
    --调用存储过程,返回指定班级名称和性别信息
    --参数传递顺序一致:第一个实参默认就是传递第一个形参。。依次略推
    --如果有默认值,那么可以使用default,或者将有默认值的参数写在所以参数列表的最后,也可以使用 参数=值的方法调用存储过程,这样就和顺序没有关系了,一旦使用了'@name = value' 形式之后,所有后续的参数就必须以'@name = value' 的形式传递
    exec usp_getAllstudentByClassName default '一班'

    4.创建带有输出参数的存储过程(返回多个值)

    (1).根据班级和性别查询学员,同时返回总人数和指定性别的人数
    if exists(select * from sysobjects where name='usp_getAllstudentAndCount')
    drop proc usp_getAllstudentAndCount
    go
    create procedure usp_getAllstudentAndCount
    @totalNum int output, --如果一个参数添加了output,那么说明:它是一个输出参数。表明outout说明了你向服务器请求返回这个参数的值
    @classNum int output, --指定班级和性别的总人数
    @className nvarchar(50),--输入参数:需要用户传入值
    @sex char(2)='男'
    as
    declare @cid = (select classid from grade where classname =@className )--根据班级名称获取班级ID
    set @classNum =( select * from student where sex = @sex and ClassId=@ClassId)
    set @totalNum =(select count(*) from student)--总人数
    go
    --调用在输出参数的存储过程
    --服务器向你返回值,用户就需要创建对应的变量就受
    --标明output 说明你会向服务器请求返回这个参数的值。而服务器也知道了标识output参数在以后需要返回
    declare @tnum int,@cnum int
    exec usp_getAllstudentByClassName @tnum output, @cnum output, '一班'
    print @tnum
    print @cnum

    5.创建有返回值的存储过程(返回一个值)
    (1)返回指定人数
    if exists(select * from sysobjects where name='usp_getStudentNameByNId')
    drop proc usp_getStudentNameByNId
    go
    create procedure usp_getStudentNameByNId
    @cid int
    as
    declare @name nvarchar(50)
    set @cnt=( select count(*) from student where classId= @cid )
    --return 只能返回整数值
    return @cnt
    go
    --调用存储过程,返回指定学号的学员姓名
    declare @count int
    exec @count = usp_getStudentNameByNId 10
    print @count

  • 相关阅读:
    02-自定义CALayer
    01-CALayer的基本操作
    抽屉效果
    手势识别
    事件响应
    寻找最合适的view
    hitTest方法与PointInside方法
    02-事件的产生与传递
    OC图标+启动图
    OC多线程操作
  • 原文地址:https://www.cnblogs.com/jinjingBlog/p/9803437.html
Copyright © 2011-2022 走看看