zoukankan      html  css  js  c++  java
  • 数据库(九)之存储过程

    存储过程概述
     什么时存储过程?
     存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储在数据库中。存储过程可包含程序流,逻辑以及对数据库得查询。它们可以接收参数,输出参数,返回单个或者多个结果集以及返回值。
     
     为什么使用存储过程?
     存储过程与其他应用程序共享应用程序逻辑,因而确保了数据访问和修改的一致性。存储过程可以封装业务功能,在存储过程中可以在同一位置改变封装的业务规则和策略。所有的客户端可以使用相同的存储过程来确保数据访问和修改的一致性。 
     存储过程具有安全性和所有权链接,以及可以附加到它们的证书。用户可以被授予权限来执行存储过程而不必直接对存储过程中引用的对象具有权限。 
     存储过程提供了安全机制。即使是没有访问存储过程引用的表或者视图的权限的用户,也可以被授权执行该存储过程。 
     存储过程允许模块化程序设计。存储过程一旦创建,以后即可在程序中调用任意多长。这可以改进应用程序的可维护性,并允许应用程序统一访问数据库。 
     存储过程可以减少网络通信流量。用户可以通过发送一个单独的语句实习一个复杂的操作,而不需要在网络上发送几百个T-SQL代码,这样减少了在服务器和客户机之间传递的请求的数量。
     
    存储过程种类
     用户自定义存储过程
     系统存储过程
     扩展存储过程
     
    创建存储过程
     语法格式:
     create proc[edure] procedure_name[;number]
     [{@parameter data_type}]
     [varying][=default][output][,...n]
     [with
     {recompile|encryption|recompile,encryption}]
     as
      sql_statement[...n]
     
     例子:
     create proc proc_student
     as
      select * from 学生信息
      
     exec proc_student
     
     例2,带参数的存储过程:
     create proc proc_student
     @sex varchar(20)
     as
     select * from 学生信息
     where 性别 = @sex
     
     exec proc_student '男'
     exec proc_student @sex = '男'
     
     例3,带默认参数的存储过程:
     create proc p_employee
     @departmentId varchar(10),
     @position varchar(10) = '职员'
     as
      select A.员工姓名, A.性别, A.所任职位, A.联系电话, B.部门名称
      from 职员信息 A, 部门信息 B
      where A.所在部门编号 = B.部门编号 and B.部门编号 = @departmentID and
      A.所任职位 = @position
     
     exec p_empolyee '003'
     
     exec p_empolyee '003', 'manager'
     
     例4,带有输出参数的存储过程
     create proc p_sorce
     @name varchar(10),
     @sorce int output
     as
      select @sorce = avg(A.分数)
      from 成绩信息 A, 学生信息 B
      where A.学生编号 = B.学号 and B.姓名 = @name
     
     declare @sorce int
     exec p_sorce '小明', @sorce
     
     例5,存储过程示例
     create proc sorceforupdate
     @id varchar(10),
     @sorce int
     as
      update 成绩信息 set 分数 = @sorce where 成绩编号 = @id
     
     exec sorceforupdate '3', 100
     
    修改存储过程
     alter proc sorceforupdate
     @id varchar(10),
     @sorce int
     as
      update 成绩信息 set 分数 = @sorce where 成绩编号 = @id
     
    删除存储过程
     语法格式:
      drop procedure{procedure}[,...n]
     
     例子:
      drop proc sorceforupdate
     
    设计存储过程的规则:
     可以引用在同一存储过程中创建的对象,只要引用时已经创建了该对象即可。
     
     可以在存储过程中引用临时表。如果在存储过程内创建本地临时表,则临时表仅为该存储过程
     存在;当退出该存储过程后,临时表将消失。
     
     如果执行的存储过程将调用另一个存储过程,则被调用的存储过程可以访问由第一个存储过程
     创建的所有对象,包括临时表在内。
     
     如果执行对远程Microsoft SQL Server 2008 实例进行更改的远程存储过程,则不能回滚这些
     更改。远程存储过程不参与事务处理。
     
     存储过程中参数的最大数目,局部变量数目,存储过程大小受可用内存的限制。
     
     存储过程中不能包括的T_SQL语句:
      create aggergate__
      create rule
      create default
      create schema
      create or alter function
      create or alter trigger
      create or alter procedure
      create or alter view
      set parseonly
      set showplan_all
      set showplan_xml
      set showplan_text
      use database_name
     
    系统存储过程
     exec sp_who
     exec sp_helpdb
     exec sp_helpdb 学生成绩管理系统
     exec sp_monitor
     
    临时存储过程
     局部的:
     create proc #get_info
     as
      select A.分数, B.姓名 from 成绩信息 a, 学生信息 B
      where a.学生编号 = B.学号
      order by B.姓名
     
     exec #get_info
     
     全局的:
     create proc ##get_info
     as
      select A.分数, B.姓名 from 成绩信息 a, 学生信息 B
      where a.学生编号 = B.学号
      order by B.姓名
     
    嵌套存储过程
     create proc get_classinfo
     as
      select * from 班级信息
      exec get_student '男'
      
     exec get_classinfo
     
    查看存储过程
     exec sp_helptext get_classinfo 
     
     
     

  • 相关阅读:
    用wifi调试android程序
    【翻译】Ext JS 4.1的性能
    index range scan,index fast full scan,index skip scan发生的条件
    柱状图(Histogram),绑定变量,bind peeking,cursor_sharing 之间的关系1 柱状图
    柱状图(Histogram),绑定变量,bind peeking,cursor_sharing 之间的关系3 柱状图与cursor_sharing
    JNI开发入门
    索引的Clustering Factor
    索引选择性
    列定义允许null,可能导致Index Hint提升不走索引
    柱状图(Histogram),绑定变量,bind peeking,cursor_sharing 之间的关系2 绑定变量与柱状图
  • 原文地址:https://www.cnblogs.com/ZGreMount/p/8612292.html
Copyright © 2011-2022 走看看