sqlserver存储过程案例教程

定义

用sql写业务逻辑,系统变了,只要数据库没变,功能还是可以生效

常用存储过程(系统自带)

--查看数据库
exec sp_databases;
-- 查看表
exec sp_tables;

-- 查看列
exec sp_columns studentInfo;
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;

大部分业务功能的实现都需要自定义存储过程,以下是一些例子

无参数存储过程

select * from sys.objects where type='p';
-- 判断是否存在
if exists (select * from sys.databases where name='mydata1') print 'yes' else print 'no';
use mydata1;
go
-- 创建不带参数的存储过程
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 studentInfo;

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

-- 查看是否有存储过程
exec sp_stored_procedures;

--修改存储过程
alter proc proc_get_student
as 
select * from studentInfo;

-- 带参存储过程,各个参数类型表示如下
--AF = 聚合函数 (CLR)
		--C = CHECK 约束
		--D = DEFAULT(约束或独立)
		--F = FOREIGN KEY 约束
		--FN = SQL 标量函数
		--FS = 程序集 (CLR) 标量函数
		--FT = 程序集 (CLR) 表值函数
		--IF = SQL 内联表值函数
		--IT = 内部表
		--P = SQL 存储过程
		--PC = 程序集 (CLR) 存储过程
		--PG = 计划指南
		--PK = PRIMARY KEY 约束
		--R = 规则(旧式,独立)
		--RF = 复制筛选过程
		--S = 系统基表
		--SN = 同义词
		--SQ = 服务队列
		--TA = 程序集 (CLR) DML 触发器
		--TF = SQL 表值函数
		--TR = SQL DML 触发器
		--U = 表(用户定义类型)
		--UQ = UNIQUE 约束
		--V = 视图
		--X = 扩展存储过程

-- 创建带参存储过程
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 
exec sp_tables;
select * from stuInfo where stuId between @startId and @endId
go
exec proc_find_stu 0,111;

-- 带通配符参数存储过程
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 stuInfo where stuInfo.stuName like @name and stuName like 
@nextname;
go
-- 传空参啥输出结果都没
exec proc_findstudentbyname;
exec proc_findstudentbyname '%1%','1%';

select * from stuInfo;

带参数存储过程

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=stuName,@age=age from stuInfo where stuId=@id and age=@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

不缓存存储过程

if object_id('proc_temp','p') is not null
drop proc proc_temp
go
create proc proc_temp 
with recompile
as 
select * from stuInfo
go

exec proc_temp;

加密存储过程

if object_id('proc_temp encription','p') is not null
drop proc proc_temp_encription
go
create proc proc_temp_encription 
with encryption
as
select * from stuInfo

exec proc_temp_encription;
exec sp_helptext 'proc_temp';
exec sp_helptext 'proc_temp_encription';

分页存储过程

---存储过程、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 student
;    
    select * from (
        select row_number() over(order by id) as rowId, * from student 
    ) 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;

游标存储过程

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;--删除游标

用户自定义错误信息

raiserror('is errror',16,1);
select * from sys.messages;

--使用sysmesssages中定义的信息
-- 16错误级别 19 到 25 之间的严重级别只能由 sysadmin 固定服务器角色成员使用。若要使用 19 到 25 之间的严重级别,必须选择 WITH LOG 选项
raiserror(33003,16,1);
raiserror(33006,16,1);

--msg_id是错误消息的ID,msg_str是自定义错误消息的文本。severity是错误的严重程度,取值范围为125,其中1表示最轻微,25表示最严重。state是错误状态,取值范围为0255,用于指示错误发生的位置。argument是错误消息中包含的参数,可以是整数、字符或日期等类型。option是其他选项,如WITH LOG、WITH NOWAIT等,用于指定如何处理错误消息。
--在代码中,RAISERROR(33003,16,1)表示生成一个自定义错误消息,该消息的ID为33003,严重程度为16,状态为1。由于msg_str参数未指定,所以错误消息的文本将从SQL Server的错误消息目录中获取。该错误消息的严重程度为16,表示一个一般错误,可以被捕获和处理,但仍需要注意。状态为1表示错误发生的位置。你可以根据自己的需要修改和扩展这个示例。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
存储过程Procedure是一组为了完成特定功能的SQL语句集合,经编译后存储在数据库中,用户通过指定存储过程的名称并给出参数来执行。 存储过程中可以包含逻辑控制语句和数据操纵语句,它可以接受参数、输出参数、返回单个或多个结果集以及返回值。 由于存储过程在创建时即在数据库服务器上进行了编译并存储在数据库中,所以存储过程运行要比单个的SQL语句块要快。同时由于在调用时只需用提供存储过程名和必要的参数信息,所以在一定程度上也可以减少网络流量、简单网络负担。 1、 存储过程的优点 A、 存储过程允许标准组件式编程 存储过程创建后可以在程序中被多次调用执行,而不必重新编写该存储过程SQL语句。而且数据库专业人员可以随时对存储过程进行修改,但对应用程序源代码却毫无影响,从而极大的提高了程序的可移植性。 B、 存储过程能够实现较快的执行速度 如果某一操作包含大量的T-SQL语句代码,分别被多次执行,那么存储过程要比批处理的执行速度快得多。因为存储过程是预编译的,在首次运行一个存储过程时,查询优化器对其进行分析、优化,并给出最终被存在系统表中的存储计划。而批处理的T-SQL语句每次运行都需要预编译和优化,所以速度就要慢一些。 C、 存储过程减轻网络流量 对于同一个针对数据库对象的操作,如果这一操作所涉及到的T-SQL语句被组织成一存储过程,那么当在客户机上调用该存储过程时,网络中传递的只是该调用语句,否则将会是多条SQL语句。从而减轻了网络流量,降低了网络负载。 D、 存储过程可被作为一种安全机制来充分利用 系统管理员可以对执行的某一个存储过程进行权限限制,从而能够实现对某些数据访问的限制,避免非授权用户对数据的访问,保证数据的安全。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超级无敌暴龙战士塔塔开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值