zoukankan      html  css  js  c++  java
  • 存储过程

    --1.什么是存储过程
    --存储过程(procedure)类似于java语言中的方法
    --用来执行管理任务或应用复杂的业务规则
    --存储过程可以带参数,也可以返回结果
    --存储过程可以包含数据操作语句、变量、逻辑 控制语句等
    --2.存储过程的优点
    --执行速度更快
    --允许模块化程序设计 
    --提高系统安全性
    --减少网络流通量
    --3.存储过程的分类
    --系统存储过程
    --由系统定义,存放在master数据库中
    --系统存储过程的名称都以“sp_”开头或”xp_”开头
    --用户自定义存储过程
    --由用户在自己的数据库中创建的存储过程
    --4.定义存储过程的语法
     --Create Procedure procedure_name
    --As
    --Sql_statement
    --4.1 创建存储过程
    create procedure p1
    as
    select productid,productname,unitprice from products
    
    --4.2调用
    execute p1
    
    --4.3存储过程的参数分两种: 输入参数 输出参数
    create procedure p2
    --定义输入输出参数
    @pname varchar(10)--注意,声明变量不需要declare
    as
    select productid,productname,unitprice from products
    where productname like '%'+@pname+'%'
    
    execute p2 'ch'
    
    create procedure p3
    @num1 int, --输入参数
    @num2 int,
    @sum int output --输出参数
    as
    set @sum = @num1 + @num2
    
    declare @s  int
    exec p3 1,1,@s output
    print @s
    
    5.1创建函数
    create function f1(@num1 int,@num2 int)
    returns int --声明函数返回类型
    as
    begin
    declare @sum int
    set @sum = @num1+@num2
    return @sum --函数的返回值
    end
    --5.2调用函数方式:函数名()
    print dbo.f1(1,1)
    
    
    --6.分页
    create procedure p4
    @pageNo int=1,
    @pageSize int=5,
    @totalPage int output
    as
    select top (@pageSize) productid,productname,unitprice from products
    where productid not in 
    (select top ((@pageNo-1)*@pageSize) productid from products)
    --总页数
    declare @total int
    select @total=count(*) from products --统计products表中的总记录数 10/5 12/5
    set @totalPage = @total / @pageSize
    if @total%@pageSize<>0
       set @totalPage=@totalPage+1
    
    --调用者得到第几页的数据和总页数
    declare @t int
    exec p4 5,3,@t output
    print @t
    
    --了解内容1
    --可以使用PRINT语句显示错误信息,但这 些信息是临时的,只能显示给用户 
    --RAISERROR 显示用户定义的错误信息时
    --可指定严重级别,
    --设置系统变量@@ERROR
    --记录所发生的错误等
    
    --了解内容1:常用的系统存储过程
  • 相关阅读:
    spark dataframe 正则表达式匹配
    JVM申请的memory不够导致无法启动SparkContext
    <scope>provided</scope> 关于maven依赖中的scope的作用和用法
    web前端网站
    元素居中
    如何在Vue项目中使用vw实现移动端适配
    微任务、宏任务、同步、异步、Promise、Async、await
    前端自动化工作流环境
    Web前端学习笔记——构建前端自动化工作流环境
    JS判断值是否是数字
  • 原文地址:https://www.cnblogs.com/kite/p/3634559.html
Copyright © 2011-2022 走看看