下面介绍十大不同类型存储过程。

用户自定义存储过程 

   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_student
go
create proc proc_get_student
as
select * from student; --调用、执行存储过程
exec proc_get_student; 3、 修改存储过程 --修改存储过程
alter proc proc_get_student
as
select * from student; 4、 带参存储过程 --带参存储过程
if (object_id('proc_find_stu', 'P') is not null)
drop proc proc_find_stu
go
create proc proc_find_stu(@startId int, @endId int)
as
select * from student where id between @startId and @endId
go exec proc_find_stu 2, 4; 5、 带通配符参数存储过程 --带通配符参数存储过程
if (object_id('proc_findStudentByName', 'P') is not null)
drop proc proc_findStudentByName
go
create proc proc_findStudentByName(@name varchar(20) = '%j%', @nextName varchar(20) = '%')
as
select * 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_getStudentRecord
go
create proc proc_getStudentRecord(
@id int, --默认输入参数
@name varchar(20) out, --输出参数
@age varchar(20) output--输入输出参数
)
as
select @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_temp
go
create proc proc_temp
with recompile
as
select * from student;
go exec proc_temp; 8、 加密存储过程 --加密WITH ENCRYPTION
if (object_id('proc_temp_encryption', 'P') is not null)
drop proc proc_temp_encryption
go
create proc proc_temp_encryption
with encryption
as
select * 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_cursor
go
create proc proc_cursor
@cur cursor varying output
as
set @cur = cursor forward_only static for
select 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)
begin
fetch next from @exec_cur into @id, @name, @age;
print 'id: ' + convert(varchar, @id) + ', name: ' + @name + ', age: ' + convert(char, @age);
end
close @exec_cur;
deallocate @exec_cur;--删除游标 10、 分页存储过程 ---存储过程、row_number完成分页
if (object_id('pro_page', 'P') is not null)
drop proc proc_cursor
go
create proc pro_page
@startIndex int,
@endIndex int
as
select count(*) from product
;
select * from (
select row_number() over(order by pid) as rowId, * from product
) temp
where temp.rowId between @startIndex and @endIndex
go
--drop proc pro_page
exec pro_page 1, 4
--
--分页存储过程
if (object_id('pro_page', 'P') is not null)
drop proc pro_stu
go
create procedure pro_stu(
@pageIndex int,
@pageSize int
)
as
declare @startRow int, @endRow int
set @startRow = (@pageIndex - 1) * @pageSize +1
set @endRow = @startRow + @pageSize -1
select * from (
select *, row_number() over (order by id asc) as number from student
) t
where t.number between @startRow and @endRow; exec pro_stu 2, 2;

SQL Server之十大存储过程的更多相关文章

  1. SQL Server DBA十大必备工具使生活轻松

    [IT168 技术]曾经和一些DBA和数据库开发人员交流时,问他们都用过一些什么样的DB方面的工具,大部分人除了SSMS和Profile之外,基本就没有使用过 其他工具了;诚然,SSMS和Profil ...

  2. SQL Server技术问题之存储过程与sql语句的优缺点

    优点: 1. 允许模块化程序设计.2.可维护性高,只需创建存储过程一次并将其存储在数据库中,以后即可在程序中调用该过程任意次.存储过程可独立于程序源代码而单独修改,而不需要更改.测试以及重新部署程序集 ...

  3. sql server 常用的系统存储过程

      系统存储过程 说明 sp_databases 列出服务上的所有数据库 sp_helpdb 报告有关指定数据库或所有数据库的信息 sp_renamedb 更改数据库的名称 sp_tables 返回当 ...

  4. Sql Server 带参数的存储过程执行方法

    Sql Server 带参数的存储过程执行方法 Visual C# 动态操作 SQL Server 数据库实例教程(4):带参数的存储过程执行方法 上一篇文章介绍了带参数的SQL语句执行方法和不带参数 ...

  5. [转] sql server 跨数据库调用存储过程

    A库存储过程: create PROCEDURE [dbo].[spAAAForTest] ( ) =null , ) =null ) AS BEGIN select N'A' AS a , N'B' ...

  6. SQL Server 数据库try catch 存储过程

    SQL Server 在生产环境中这样写存储过程的坑都避免了吗? 原文链接: http://www.cnblogs.com/chenmh/p/7856777.html 概述 最近因为业务的需求写了一段 ...

  7. SQL SERVER 2008破解加密存储过程(修正存储过程过长解密出来是空白的问题)

    SQLServer2005里使用with encryption选项创建的存储过程仍然和sqlserver2000里一样,都是使用XOR进行了的加密.和2000不一样的是,在2005的系统表syscom ...

  8. mysql、sql server、oracle大比较

    MYSQL 多个数据库多个用户形式(最好每个数据库对应一个用户),占用内存小,适用于所有平台,开源免费 客户端和命令窗口,都是由数据库决定内容-> use datebase; 组函数在selec ...

  9. sql server 常用的扩展存储过程

    sql server 里面提供了丰富的系统存储过程来辅助我们管理数据库以及开发.今天分享介绍一些常用的数据库扩展存储过程 xp_cmdshell 这个大家都比较熟悉了,使用xp_cmdshell 可以 ...

随机推荐

  1. BZOJ 1232 USACO 2008 Nov. 安慰奶牛Cheer

    [题解] 对于每一条边,我们通过它需要花费的代价是边权的两倍加上这条边两个端点的点权. 我们把每条边的边权设为上述的值,然后跑一边最小生成树,再把答案加上最小的点权就好了. #include<c ...

  2. js 保留几位小数位数

    定义和用法 toFixed() 方法可把 Number 四舍五入为指定小数位数的数字.

  3. echarts demo

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. 背包again

    Gy最近学习了01背包问题,无聊的他又想到了一个新的问题,给定n个物品的价值,和01背包一样,每个物品只能选1次或0次,求最小不能被得到的价值. 输入 第一行一个正整数T(T <= 100),表 ...

  5. [jdoj1258]野生动物园(change by panxf)_权值线段树_组合数

    人品计算 题目大意:n个数的a序列,m组询问.每次询问给出T,A,B,K.求在a序列的[A,B]的位置之内的K小值P,的$C_{T}^{P \% T} \% 10111$. 注释:每组询问保证区间只相 ...

  6. [Node.js] Add Logging to a Node.js Application using Winston

    Winston is a popular logging library for NodeJS which allows you to customise the output, as well as ...

  7. [CSS3] Use Sticky Positioning for Section Headers

    We can take advantage of sticky positioning to keep a section header at the top of the page while th ...

  8. leetcode 二分法 Pow(x, n)

    Pow(x, n) Total Accepted: 25273 Total Submissions: 97470My Submissions Implement pow(x, n). 题意:求x的n次 ...

  9. Verilog与SystemVerilog编程陷阱:怎样避免101个常犯的编码错误

    这篇是计算机类的优质预售推荐>>>><Verilog与SystemVerilog编程陷阱:怎样避免101个常犯的编码错误> 编辑推荐 纠错式学习,从"陷阱 ...

  10. JavaI/O 系统

    1.JavaI/O 系统概述 A.  输入输出(I/O):指的是计算机与外部世界,或者一个程序与计算机的其余部分之间的接口 B. 流的概念(流:Stream) 流的基本特性:有数据.有方向 2. 流的 ...