您好,欢迎访问一九零五行业门户网

sqlserver自定义函数与存储过程的区别 实例详解

分享下sql server自定义函数与存储过程的区别,一起来学习下。 一、自定义函数: 1. 可以返回表变量 2. 限制颇多,包括 不能使用output参数; 不能用临时表; 函数内部的操作不能影响到外部环境; 不能通过select返回结果集; 不能update,delete,数据库表
分享下sql server自定义函数与存储过程的区别,一起来学习下。 
一、自定义函数:
1. 可以返回表变量
2. 限制颇多,包括
不能使用output参数;
不能用临时表;
函数内部的操作不能影响到外部环境;
不能通过select返回结果集;
不能update,delete,数据库表;
3. 必须return 一个标量值或表变量
自定义函数一般用在复用度高,功能简单单一,争对性强的地方。
二、存储过程
1. 不能返回表变量
2. 限制少,可以执行对数据库表的操作,可以返回数据集
3. 可以return一个标量值,也可以省略return
 存储过程一般用在实现复杂的功能,数据操纵方面。
=========================================================================
sqlserver存储过程--实例
实例1:只返回单一记录集的存储过程。
表银行存款表(bankmoney)的内容如下
id
userid
sex
money
001
zhangsan

30
002
wangwu

50
003
zhangsan

40
要求1:查询表bankmoney的内容的存储过程
create procedure sp_query_bankmoney
as
select * from bankmoney
go
exec sp_query_bankmoney
注* 在使用过程中只需要把t-sql中的sql语句替换为存储过程名,就可以了很方便吧!
实例2(向存储过程中传递参数):
加入一笔记录到表bankmoney,并查询此表中userid= zhangsan的所有存款的总金额。
create proc insert_bank @param1 char(10),@param2 varchar(20),@param3 varchar(20),@param4 int,@param5 int output
with encryption ---------加密
as --- www.jbxue.com
insert into bankmoney (id,userid,sex,money)
values(@param1,@param2,@param3, @param4)
select @param5=sum(money) from bankmoney where userid='zhangsan'
go
在sql server查询分析器中执行该存储过程的方法是:
declare @total_price int
exec insert_bank '004','zhangsan','男',100,@total_price output
print '总余额为'+convert(varchar,@total_price)
go
在这里再啰嗦一下存储过程的3种传回值(方便正在看这个例子的朋友不用再去查看语法内容):
1.以return传回整数
2.以output格式传回参数
3.recordset
传回值的区别:
output和return都可在批次程式中用变量接收,而recordset则传回到执行批次的客户端中。
实例3:使用带有复杂 select 语句的简单过程
下面的存储过程从四个表的联接中返回所有作者(提供了姓名)、出版的书籍以及出版社。该存储过程不使用任何参数。
use pubs
if exists (select name from sysobjects
where name = 'au_info_all' and type = 'p')
drop procedure au_info_all
go
create procedure au_info_all
as
select au_lname, au_fname, title, pub_name
from authors a inner join titleauthor ta
on a.au_id = ta.au_id inner join titles t
on t.title_id = ta.title_id inner join publishers p
on t.pub_id = p.pub_id
go
au_info_all 存储过程可以通过以下方法执行:
execute au_info_all
-- or
exec au_info_all
如果该过程是批处理中的第一条语句,则可使用:
au_info_all
实例4:使用带有参数的简单过程
create procedure au_info
@lastname varchar(40),
@firstname varchar(20)
as
select au_lname, au_fname, title, pub_name
from authors a inner join titleauthor ta
on a.au_id = ta.au_id inner join titles t
on t.title_id = ta.title_id inner join publishers p
on t.pub_id = p.pub_id
where au_fname = @firstname
and au_lname = @lastname
go
au_info 存储过程可以通过以下方法执行:
execute au_info 'dull', 'ann'
-- or
execute au_info @lastname = 'dull', @firstname = 'ann'
-- or
execute au_info @firstname = 'ann', @lastname = 'dull'
-- or
exec au_info 'dull', 'ann'
-- or
exec au_info @lastname = 'dull', @firstname = 'ann'
-- or
exec au_info @firstname = 'ann', @lastname = 'dull'
如果该过程是批处理中的第一条语句,则可使用:
au_info 'dull', 'ann'
-- or
au_info @lastname = 'dull', @firstname = 'ann'
-- or
au_info @firstname = 'ann', @lastname = 'dull'
实例5:使用带有通配符参数的简单过程
create procedure au_info2
@lastname varchar(30) = 'd%',
@firstname varchar(18) = '%'
as
select au_lname, au_fname, title, pub_name
from authors a inner join titleauthor ta
on a.au_id = ta.au_id inner join titles t
on t.title_id = ta.title_id inner join publishers p
on t.pub_id = p.pub_id
where au_fname like @firstname
and au_lname like @lastname
go
au_info2 存储过程可以用多种组合执行。下面只列出了部分组合:
execute au_info2
-- or
execute au_info2 'wh%'
-- or
execute au_info2 @firstname = 'a%'
-- or
execute au_info2 '[ck]ars[oe]n'
-- or
execute au_info2 'hunter', 'sheryl'
-- or
execute au_info2 'h%', 's%'
= 'proc2'
实例6:if...else
存储过程,其中@case作为执行update的选择依据,用if...else实现执行时根据传入的参数执行不同的修改.
--下面是if……else的存储过程:
if exists (select 1 from sysobjects where name = 'student' and type ='u' )
drop table student
go
if exists (select 1 from sysobjects where name = 'spupdatestudent' and type ='p' )
drop proc spupdatestudent
go
create table student
(
fname nvarchar (10),
fage
smallint ,
fdiqu varchar (50),
ftel int
)
go
insert into student values ('x.x.y' , 28, 'tesing' , 888888)
go
create proc spupdatestudent
(
@fcase int ,
@fname nvarchar (10),
@fage smallint ,
@fdiqu varchar (50),
@ftel int
)
as
update student
set fage = @fage, -- 传 1,2,3 都要更新 fage 不需要用 case
fdiqu = (case when @fcase = 2 or @fcase = 3 then @fdiqu else fdiqu end ),
ftel = (case when @fcase = 3 then @ftel else ftel end )
where fname = @fname
select * from student
go
-- 只改 age
exec spupdatestudent
@fcase = 1,
@fname = n'x.x.y' ,
@fage = 80,
@fdiqu = n'update' ,
@ftel = 1010101
-- 改 age 和 diqu
exec spupdatestudent
@fcase = 2,
@fname = n'x.x.y' ,
@fage = 80,
@fdiqu = n'update' ,
@ftel = 1010101
-- 全改
exec spupdatestudent
@fcase = 3,
@fname = n'x.x.y' ,
@fage = 80,
@fdiqu = n'update' ,
@ftel = 1010101
其它类似信息

推荐信息