sqlserver 多語句表值函數例子


多語句表值函數顧名思義是返回了一張表,可以傳入多個參數

 

 

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);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM