SQL Server存储过程
一, 存储过程是什么
存储过程是一个预编译的SQL语句,优点是允许模块化的设计,就是说只需创建一次,以后在程序中就可以调用多次。如果某次操作需要执行多次SQL,使用存储过程比单纯SQL语句执行要快。可以用一个“execute 存储过程名 参数”命令来调用存储过程。
二,存储过程的优缺点
优势:响应时间上来说有优势,可以给我们带来运行效率提高的好处,且使用存储过程的系统更加稳定
缺点:维护性较差,相对于简单sql,存储过程并没有什么优势,并且在进行调试时比较困难
三,创建存储过程
查询
if (exists (select * from sys.objects where name = 'GetUser')) drop proc GetUser --判断存储过程是否存在,存在则删除然后重建。
go
create proc GetUser --创建存储过程 GetUser
@Id int --参数
as
set nocount on; --不返回计数,提高应用程序性能
begin --开始
select * from [dbo].[User] where Id=@Id --执行sql语句
end;--结束
if (exists (select * from sys.objects where name = 'GetUser')) drop proc GetUser --判断存储过程是否存在,存在则删除然后重建。
go
create proc GetUser --创建存储过程 GetUser
@Id int --参数
as
set nocount on; --不返回计数,提高应用程序性能
begin --开始
select * from [dbo].[User] where Id=@Id --执行sql语句
end;--结束
调用存储过程
EXEC GetUser 1;