• sql笔记(2)存储过程


    create database company--建立数据库
    go

    create table stu_info--建表
    (
     stu_id int primary key,
     stu_name varchar(30) not null,
     stu_sex char(2) ,
     salary money ,
     birthday smalldatetime
    )
    go

       --插入数据
    insert into stu_info values(1,'张三','男',1000,'2000-10-10')
    insert into stu_info values(2,'李四','女',2000,'2001-10-10')
    insert into stu_info values(3,'王五','男',3000,'2002-10-10')
    insert into stu_info values(4,'张六','男',4000,'2003-10-10')
    go
    select * from stu_info
    ---------------------------------------------------
    --创建存储过程
    ------------------------------------------------
    --11111创建(一个无参无返的)存储过程
    create proc wfwc
    as
    select * from stu_info

    --执行 存储过程 exec 存储名
    exec wfwc     
    drop proc  wfwc--删除存储过程 drop proc 存储名
    --22222创建(有输入参数的)存储过程
    create proc wfyc
    @salary int = 1000,   --整形的     无默认值的    输入参数
    @sex varchar(2) = '女'--字符类型的 无默认值的    输入参数
    as
    select * from stu_info where salary > @salary and stu_sex = @sex

    exec wfyc --执行的是默认参数值的(此时是有默认参数的,若没有默认参数的,在执行的过程中必须传参 且数据类型一致)
    exec wfyc 2000,'男' --执行传过去的参数 此时传过去的参数又先后顺序的
    exec wfyc @salary=2000,@sex='男'
    --或者
    exec wfyc @sex='男',@salary=2000

    --修改(上面的)存储过程 由 有默认值的 修改为 无默认值的
    alter procedure wfyc
    @salary int,
    @sex varchar(10)
    as
    select * from stu_info where salary>@salary and stu_sex=@sex

    exec wfyc 1000,'男' --此时必须有参数

    drop procedure  wfyc
    --33333新建一个(有输入、输出参数的) 存储过程
    create procedure wfyc_output
    @salary int,
    @sex varchar(10),
    @count int output--多了一个输出参数   与输入参数不同的是 后面 有 output
    as
    set @count=
    (select count(*) from stu_info where salary>@salary and stu_sex=@sex)

    --sql执行语句
    declare @num int--声明局部变量
    execute wfyc_output 1000,'男',@num output--执行有输出参数的存储过程时,输出参数比较特殊  注意区别: 事先声明的一个同类型的变量 output
    select @num as N'性别是"男",工资超过"1000"的员工数' --打印出   变量的值
    --以上三句须同时执行
    drop  procedure wfyc_output---删除存储过程

    --44444存储过程(有输入输出参数的 有返回值的)
    create procedure yfyc__output
    @salary int,
    @sex varchar(10),
    @count int output
    as
    set @count= --set 赋值
    (select count(*) from stu_info where salary>@salary and stu_sex=@sex)
    if @count<>0 
    begin 
     return 1
    end
    else
    begin 
     return 0
    end
    --sql执行
    declare @count int,@result int--声明变量( 只要有声明的变量  和此变量有关系的执行存储过程 都要同时执行)
    exec @result=yfyc__output 1000,'男',@count output--执行存储过程
    print @count  --打印出 符合要求的查询的 数目
    print @result --打印出 执行存储过程时的返回值
    drop proc yfyc__output

    --创建存储过程(分页)
    create proc select_stu_info10
    --页码 2,每页记录数 2
    @pageNumer int=2,--不管总共有多少条记录,前面按记录数分页,最后剩下不够记录数的也算一页
    @pageSize int=2
    as select top (@pageSize) *from stu_info --top 2 指每页记录数 即@pageSize ()不能少
    where stu_id not in(select top ((@pageNumer-1)*@pageSize)stu_id from stu_info order by stu_id asc)
    exec select_stu_info10 --显示最后一页的记录数据
    select *from stu_info

  • 相关阅读:
    背包——[Usaco2007 Jan]Running POJ3661
    KMP的next函数——BZOJ1355
    对括号匹配问题的在深入思考
    优先队列——[Usaco2009 Open]工作安排Job
    完全背包——poj 3181
    奇异数
    背包计数
    最大递增子序和——POJ3616
    大组合数取模——fzu 2020(可做模板)
    activemq 代码库
  • 原文地址:https://www.cnblogs.com/top100/p/2092743.html
Copyright © 2020-2023  润新知