首页 > 数据库技术 > 详细

sqlserver 多语句表值函数例子

时间:2020-02-02 20:19:57      阅读:109      评论:0      收藏:0      [点我收藏+]

多语句表值函数顾名思义是返回了一张表,可以传入多个参数

技术分享图片

 

 

1.定义

 1 Use AdventureWorks2014;
 2 go
 3 if exists(select * from sys.objects where name=udf_SEL_SalesQuota)
 4 drop function dbo.udf_SEL_SalesQuota;
 5 go
 6 CREATE FUNCTION dbo.udf_SEL_SalesQuota ( @BusinessEntityID int, @ShowHistory bit )
 7 RETURNS @SalesQuota TABLE 
 8     (
 9         BusinessEntityID int, 
10         QuotaDate datetime, 
11         SalesQuota money
12     )
13 as
14 begin
15     INSERT Into @SalesQuota(BusinessEntityID, QuotaDate, SalesQuota)
16     SELECT BusinessEntityID, ModifiedDate, SalesQuota
17     FROM Sales.SalesPerson
18     WHERE BusinessEntityID = @BusinessEntityID;
19 
20     IF @ShowHistory = 1
21          begin
22             INSERT Into @SalesQuota(BusinessEntityID, QuotaDate, SalesQuota)
23             SELECT BusinessEntityID, QuotaDate, SalesQuota
24             FROM Sales.SalesPersonQuotaHistory
25             WHERE BusinessEntityID = @BusinessEntityID;
26          end
27 
28     return
29 
30 end

2.调用

1 Use AdventureWorks2014;
2 GO
3 SELECT BusinessEntityID, QuotaDate, SalesQuota      
4 FROM dbo.udf_SEL_SalesQuota (275,0);

 

sqlserver 多语句表值函数例子

原文:https://www.cnblogs.com/Spinoza/p/12253043.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!