Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用。当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句。这样就可以提高存储过程的性能。

Ø 存储过程的概念

存储过程Procedure是一组为了完成特定功能的SQL语句集合,经编译后存储在数据库中,用户通过指定存储过程的名称并给出参数来执行。

存储过程中可以包含逻辑控制语句和数据操纵语句,它可以接受参数、输出参数、返回单个或多个结果集以及返回值。

由于存储过程在创建时即在数据库服务器上进行了编译并存储在数据库中,所以存储过程运行要比单个的SQL语句块要快。同时由于在调用时只需用提供存储过程名和必要的参数信息,所以在一定程度上也可以减少网络流量、简单网络负担。

1、 存储过程的优点

A、 存储过程允许标准组件式编程

存储过程创建后可以在程序中被多次调用执行,而不必重新编写该存储过程的SQL语句。而且数据库专业人员可以随时对存储过程进行修改,但对应用程序源代码却毫无影响,从而极大的提高了程序的可移植性。

B、 存储过程能够实现较快的执行速度

如果某一操作包含大量的T-SQL语句代码,分别被多次执行,那么存储过程要比批处理的执行速度快得多。因为存储过程是预编译的,在首次运行一个存储过程 时,查询优化器对其进行分析、优化,并给出最终被存在系统表中的存储计划。而批处理的T-SQL语句每次运行都需要预编译和优化,所以速度就要慢一些。

C、 存储过程减轻网络流量

对于同一个针对数据库对象的操作,如果这一操作所涉及到的T-SQL语句被组织成一存储过程,那么当在客户机上调用该存储过程时,网络中传递的只是该调用语句,否则将会是多条SQL语句。从而减轻了网络流量,降低了网络负载。

D、 存储过程可被作为一种安全机制来充分利用

系统管理员可以对执行的某一个存储过程进行权限限制,从而能够实现对某些数据访问的限制,避免非授权用户对数据的访问,保证数据的安全。

Ø 系统存储过程

系统存储过程是系统创建的存储过程,目的在于能够方便的从系统表中查询信息或完成与更新数据库表相关的管理任务或其他的系统管理任务。系统存储过程主要存 储在master数据库中,以“sp”下划线开头的存储过程。尽管这些系统存储过程在master数据库中,但我们在其他数据库还是可以调用系统存储过 程。有一些系统存储过程会在创建新的数据库的时候被自动创建在当前数据库中。

常用系统存储过程有:

exec sp_databases; --查看数据库exec sp_tables;        --查看表exec sp_columns student;--查看列exec sp_helpIndex student;--查看索引exec sp_helpConstraint student;--约束exec sp_stored_procedures;exec sp_helptext 'sp_stored_procedures';--查看存储过程创建、定义语句exec sp_rename student, stuInfo;--修改表、索引、列的名称exec sp_renamedb myTempDB, myDB;--更改数据库名称exec sp_defaultdb 'master', 'myDB';--更改登录名的默认数据库exec sp_helpdb;--数据库帮助,查询数据库信息exec sp_helpdb master;


    系统存储过程示例:
--表重命名exec sp_rename 'stu', 'stud';select * from stud;--列重命名exec sp_rename 'stud.name', 'sName', 'column';exec sp_help 'stud';--重命名索引exec sp_rename N'student.idx_cid', N'idx_cidd', N'index';exec sp_help 'student';

--查询所有存储过程select * from sys.objects where type = 'P';select * from sys.objects where type_desc like '%pro%' and name like 'sp%';

Ø 用户自定义存储过程

1、 创建语法

create proc | procedure pro_name    [{@参数数据类型} [=默认值] [output],     {@参数数据类型} [=默认值] [output],     ....    ]as    SQL_statements

2、 创建不带参数存储过程

--创建存储过程if (exists (select * from sys.objects where name = 'proc_get_student'))drop proc proc_get_studentgocreate proc proc_get_studentasselect * from student;

--调用、执行存储过程exec proc_get_student;

3、 修改存储过程

--修改存储过程alter proc proc_get_studentasselect * from student;

4、 带参存储过程

--带参存储过程if (object_id('proc_find_stu', 'P') is not null)drop proc proc_find_stugocreate proc proc_find_stu(@startId int, @endId int)asselect * from student where id between @startId and @endIdgo

exec proc_find_stu 2, 4;

5、 带通配符参数存储过程

--带通配符参数存储过程if (object_id('proc_findStudentByName', 'P') is not null)drop proc proc_findStudentByNamegocreate proc proc_findStudentByName(@name varchar(20) = '%j%', @nextName varchar(20) = '%')asselect * from student where name like @name and name like @nextName;go

exec proc_findStudentByName;exec proc_findStudentByName '%o%', 't%';

6、 带输出参数存储过程

if (object_id('proc_getStudentRecord', 'P') is not null)drop proc proc_getStudentRecordgocreate proc proc_getStudentRecord(    @id int, --默认输入参数    @name varchar(20) out, --输出参数    @age varchar(20) output--输入输出参数)asselect @name = name, @age = age  from student where id = @id and sex = @age;go

-- declare @id int,        @name varchar(20),        @temp varchar(20);set @id = 7; set @temp = 1;exec proc_getStudentRecord @id, @name out, @temp output;select @name, @temp;print @name + '#' + @temp;

7、 不缓存存储过程

--WITH RECOMPILE 不缓存if (object_id('proc_temp', 'P') is not null)drop proc proc_tempgocreate proc proc_tempwith recompileasselect * from student;go

exec proc_temp;

8、 加密存储过程

--加密WITH ENCRYPTION if (object_id('proc_temp_encryption', 'P') is not null)drop proc proc_temp_encryptiongocreate proc proc_temp_encryptionwith encryptionasselect * from student;go

exec proc_temp_encryption;exec sp_helptext 'proc_temp';exec sp_helptext 'proc_temp_encryption';

9、 带游标参数存储过程

if (object_id('proc_cursor', 'P') is not null)drop proc proc_cursorgocreate proc proc_cursor    @cur cursor varying outputasset @cur = cursor forward_only static forselect id, name, age from student;open @cur;go--调用declare @exec_cur cursor;declare @id int,        @name varchar(20),        @age int;exec proc_cursor @cur = @exec_cur output;--调用存储过程fetch next from @exec_cur into @id, @name, @age;while (@@fetch_status = 0)beginfetch next from @exec_cur into @id, @name, @age;print 'id: ' + convert(varchar, @id) + ', name: ' + @name + ', age: ' + convert(char, @age);endclose @exec_cur;deallocate @exec_cur;--删除游标

10、 分页存储过程

---存储过程、row_number完成分页if (object_id('pro_page', 'P') is not null)drop proc proc_cursorgocreate proc pro_page    @startIndex int,    @endIndex intasselect count(*) from product;    select * from (select row_number() over(order by pid) as rowId, * from product     ) tempwhere temp.rowId between @startIndex and @endIndexgo--drop proc pro_pageexec pro_page 1, 4----分页存储过程if (object_id('pro_page', 'P') is not null)drop proc pro_stugocreate procedure pro_stu(    @pageIndex int,    @pageSize int)asdeclare @startRow int, @endRow intset @startRow = (@pageIndex - 1) * @pageSize +1set @endRow = @startRow + @pageSize -1select * from (select *, row_number() over (order by id asc) as number from student     ) twhere t.number between @startRow and @endRow;

exec pro_stu 2, 2;

Ø Raiserror

Raiserror返回用户定义的错误信息,可以指定严重级别,设置系统变量记录所发生的错误。

语法如下:

Raiserror({msg_id | msg_str | @local_variable}  {, severity, state}  [,argument[,…n]]  [with option[,…n]])

# msg_id:在sysmessages系统表中指定的用户定义错误信息

# msg_str:用户定义的信息,信息最大长度在2047个字符。

# severity:用户定义与该消息关联的严重级别。当使用msg_id引发使用sp_addmessage创建的用户定义消息时,raiserror上指定严重性将覆盖sp_addmessage中定义的严重性。

任何用户可以指定0-18直接的严重级别。只有sysadmin固定服务器角色常用或具有alter trace权限的用户才能指定19-25直接的严重级别。19-25之间的安全级别需要使用with log选项。

# state:介于1至127直接的任何整数。State默认值是1。

raiserror('is error', 16, 1);select * from sys.messages;--使用sysmessages中定义的消息raiserror(33003, 16, 1);raiserror(33006, 16, 1);

  • 作者:hoojo
    出处:http://www.cnblogs.com/hoojo/archive/2011/07/19/2110862.html#top
    blog:http://blog.csdn.net/IBM_hoojo
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

转载于:https://www.cnblogs.com/zxx193/p/3394763.html

SQL Server存储过程(转载)相关推荐

  1. SQL Server存储过程输入参数使用表值

    在2008之前如果我们想要将表作为输入参数传递给SQL Server存储过程使比较困难的,可能需要很多的逻辑处理将这些表数据作为字符串或者XML传入. 在2008中提供了表值参数.使用表值参数,可以不 ...

  2. Microsoft SQL Server 存储过程

    Microsoft SQL Server 存储过程 TRIGGER DDL触发器:主要用于防止对数据库架构.视图.表.存储过程等进行的某些修改:DDL事件是指对数据库CREATE,ALTER,DROP ...

  3. SQL Server存储过程中使用表值作为输入参数示例

    这篇文章主要介绍了SQL Server存储过程中使用表值作为输入参数示例,使用表值参数,可以不必创建临时表或许多参数,即可向 Transact-SQL 语句或例程(如存储过程或函数)发送多行数据,这样 ...

  4. SQL Server存储过程里全库查找引用的数据库对象(表、存储过程等)

    SQL Server存储过程全库匹配数据库对象(表.存储过程等) 简介 可以通过自定义存储过程sp_eachdb来遍历每个数据库然后结合sys.objects 关联sys.sql_modules后的d ...

  5. SQL server 存储过程的建立和调用

    SQL server 存储过程的建立和调用 存储过程的建立和调用 --1.1准备测试需要的数据库:test,数据表:物料表,采购表 if not exists (select * from maste ...

  6. java调用存储过程 sql server_Java中调用SQL Server存储过程示例

    Java中调用SQL Server存储过程示例2007-09-03 08:48来源:论坛整理作者:孟子E章责任编辑:方舟·yesky评论(3) 最近做了个Java的小项目(第一次写Java的项目哦), ...

  7. db2 删除存储过程_数据库教程-SQL Server存储过程使用及异常处理

    SQL Server存储过程 存储过程(Procedure)是数据库重要对象之一,也是数据库学习的重点之一.本文,我们以SQL Server为例对存储过程的概念.定义.调用.删除及存储过程调用异常等通 ...

  8. SQL Server存储过程初学者

    In this article, we will learn how to create stored procedures in SQL Server with different examples ...

  9. oracle如何调试sql,调试oracle与调试sql server存储过程

    [IT168 技术]关于存储过程的调试,知道方法以后很简单,但在不知道的时候,为了测试一个存储过程的正性,print,插入临时表等可谓是使出了浑身解数,烦不胜烦.下面就把我工作中调试oracle存储过 ...

  10. SQL Server 存储过程中使用raiserror抛出异常

    转自(SQL Server 存储过程中使用raiserror抛出异常 ) 一 系统预定义错误代码 SQL Server 有3831个预定义错误代码,由master.dbo.sysmessages 表维 ...

最新文章

  1. 在Linux命令行下查询当前所使用的shell版本与种类的方法
  2. opencv实现图像的拼接功能
  3. 为什么你应该让你的孩子尽早学习编程
  4. 深拷贝的缺点_拷贝?还傻傻分不清深浅?
  5. java正则表达式 分词_[Java]使用正则表达式实现分词
  6. 收获,不止SQL优化——抓住SQL的本质--第十四章
  7. 带你掌握C++中三种类成员初始化方式
  8. Selenimu做爬虫 - oscarxie - 博客园
  9. winpcap的使用
  10. redis 常用配置文件配置
  11. 我们的后花园需要如何保护
  12. python爬虫——爬取taptap游戏的评论信息(通过fiddler抓包)
  13. rk3128 平台rk818电源管理驱动移植
  14. 数据竞赛:工业互联网算法大赛能源赛道风机轴承剩余寿命预测
  15. iOS常用RGB颜色的色值一览表
  16. IDEA的快捷键与qq的冲突了怎么办
  17. [历朝通俗演义-蔡东藩-前汉]第007回 寻生路徐市垦荒 从逆谋李斯矫诏
  18. android 本地阅读pdf,Android 打开本地pdf文件
  19. Vue项目在页面添加水印功能
  20. LeetCode1221.分割平衡字符串1894.找到需要补充粉笔的学生编号(C++)

热门文章

  1. php mysql事务处理回滚操作
  2. Android 手把手教您自定义ViewGroup
  3. hadoop MapReduce实例解析
  4. 汇编语言--CPU对外设的控制
  5. return 语句不可返回指向“栈内存”的“指针”
  6. 内存分配成功,但并未初始化
  7. Informatica PowerCenter使用介绍-转载
  8. nginx访问控制:如何通过map来控制http_x_forwarded_for访问限制
  9. ML 徒手系列 最大似然估计
  10. 关于linux特殊重定向的理解