SQL Server 存储过程示例

本文详细介绍了SQL Server存储过程的使用,包括不拼接SQL字符串实现多条件查询、基本语法、触发器、自定义函数与存储过程的区别,以及多个存储过程实例,如参数传递、带有复杂SELECT语句的过程等。
摘要由CSDN通过智能技术生成
 
--有输入参数的存储过程--
create proc GetComment
(@commentid int)
as
select * from Comment where CommentID=@commentid
 
--有输入与输出参数的存储过程--
create proc GetCommentCount
@newsid int,
@count int output
as
select @count=count(*) from Comment where NewsID=@newsid
 
 
--返回单个值的函数--
create function MyFunction
(@newsid int)
returns int
as
begin
declare @count int
select @count=count(*) from Comment where NewsID=@newsid
return @count
end
 
--调用方法--
declare @count int
exec @count=MyFunction 2
print @count
 
--返回值为表的函数--
Create function GetFunctionTable
(@newsid int)
returns table
as
return
(select * from Comment where NewsID=@newsid)
 
--返回值为表的函数的调用--
select * from GetFunctionTable(2)

 SQLServer 存储过程中不拼接SQL字符串实现多条件查询

--以前拼接的写法
  set @sql=' select * from table where 1=1 '
  if (@addDate is not null)
   set @sql = @sql+' and addDate = '+ @addDate + ' '
  if (@name <>'' and is not null)
   set @sql = @sql+ ' and name = ' + @name + ' '
  exec(@sql)
下面是 不采用拼接SQL字符串实现多条件查询的解决方案

  --第一种写法是 感觉代码有些冗余
  if (@addDate is not null) and (@name <> '')
   select * from table where addDate = @addDate and name = @name
  else if (@addDate is not null) and (@name ='')
   select * from table where addDate = @addDate
  else if(@addDate is null) and (@name <> '')
   select * from table where and name = @name
  else if(@addDate is null) and (@name = '')
  select * from table
  --第二种写法是
  select * from table where (addDate = @addDate or @addDate is null) and (name = @name or @name = '')
  --第三种写法是
  SELECT * FROM table where
  addDate = CASE @addDate IS NULL THEN addDate ELSE @addDate END,
  name = CASE @name WHEN '' THEN name ELSE @name END

SQLSERVER存储过程基本语法

一、定义变量

--简单赋值
declare  @a  int
set  @a=5
print @a
  
--使用select语句赋值
declare  @user1 nvarchar(50)
select  @user1= '张三'
print @user1
declare  @user2 nvarchar(50)
select  @user2 =  Name  from  ST_User  where  ID=1
print @user2
  
--使用update语句赋值
declare  @user3 nvarchar(50)
update  ST_User  set  @user3 =  Name  where  ID=1
print @user3
 
二、表、临时表、表变量

--创建临时表1
create  table  #DU_User1
(
      [ID] [ int ]   NOT  NULL ,
      [Oid] [ int ]  NOT  NULL ,
      [Login] [nvarchar](50)  NOT  NULL ,
      [Rtx] [nvarchar](4)  NOT  NULL ,
      [ Name ] [nvarchar](5)  NOT  NULL ,
      [ Password ] [nvarchar]( max )  NULL ,
      [State] [nvarchar](8)  NOT  NULL
);
--向临时表1插入一条记录
insert  into  #DU_User1 (ID,Oid,[Login],Rtx, Name ,[ Password ],State)  values  (100,2, 'LS' , '0000' , '临时' , '321' , '特殊' );
  
--从ST_User查询数据,填充至新生成的临时表
select  *  into  #DU_User2  from  ST_User  where  ID<8
  
--查询并联合两临时表
select  *  from  #DU_User2  where  ID<3  union  select  *  from  #DU_User1
  
--删除两临时表
drop  table  #D
存储过程Procedure是一组为了完成特定功能的SQL语句集合,经编译后存储在数据库中,用户通过指定存储过程的名称并给出参数来执行。 存储过程中可以包含逻辑控制语句和数据操纵语句,它可以接受参数、输出参数、返回单个或多个结果集以及返回值。 由于存储过程在创建时即在数据库服务器上进行了编译并存储在数据库中,所以存储过程运行要比单个的SQL语句块要快。同时由于在调用时只需用提供存储过程名和必要的参数信息,所以在一定程度上也可以减少网络流量、简单网络负担。 1、 存储过程的优点 A、 存储过程允许标准组件式编程 存储过程创建后可以在程序中被多次调用执行,而不必重新编写该存储过程SQL语句。而且数据库专业人员可以随时对存储过程进行修改,但对应用程序源代码却毫无影响,从而极大的提高了程序的可移植性。 B、 存储过程能够实现较快的执行速度 如果某一操作包含大量的T-SQL语句代码,分别被多次执行,那么存储过程要比批处理的执行速度快得多。因为存储过程是预编译的,在首次运行一个存储过程时,查询优化器对其进行分析、优化,并给出最终被存在系统表中的存储计划。而批处理的T-SQL语句每次运行都需要预编译和优化,所以速度就要慢一些。 C、 存储过程减轻网络流量 对于同一个针对数据库对象的操作,如果这一操作所涉及到的T-SQL语句被组织成一存储过程,那么当在客户机上调用该存储过程时,网络中传递的只是该调用语句,否则将会是多条SQL语句。从而减轻了网络流量,降低了网络负载。 D、 存储过程可被作为一种安全机制来充分利用 系统管理员可以对执行的某一个存储过程进行权限限制,从而能够实现对某些数据访问的限制,避免非授权用户对数据的访问,保证数据的安全。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值