SQL Serversqlserver自定义函数和仓储过程的分别 实例详解

享受下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

相关文章