内联表值函数和多语句表值函数的区别:
内联表值函数,RETURNS 子句只包含关键字 table。不必定义返回变量的格式,因为它由 RETURN 子句中的 SELECT 语句 的结果集的格式设置。
内联表值函数的 函数体 不用 BEGIN 和 END 分隔。
内联表值函数的 RETURN 子句在括号中包含单个 SELECT 语句。SELECT 语句的结果集构成函数所返回的表。其使用的SELECT 语句与视图中使用的 SELECT 语句受到相同的限制。
例子:
1 go 2 create function fn_fenye 3 (@pageIndex as int, @pageSize as int) returns table 4 as 5 return 6 select 7 * 8 from 9 --vw_FullStu 是一个视图,在视图那部分里有定义,函数内可以调用视图 10 vw_FullStu 11 where 12 num between (@pageIndex - 1) * @pageSize + 1 13 and 14 @pageIndex * @pageSize; 15 go 16 --调用 17 select * from fn_fenye(3,7);
1 --定义: 2 3 SET ANSI_NULLS ON 4 GO 5 SET QUOTED_IDENTIFIER ON 6 GO 7 -- ============================================= 8 -- Author: zyp 9 -- Create date: 2012-01-09 10 -- Description: 盗版我们更专业!!! 11 -- ============================================= 12 Create FUNCTION fn_FunGetMaterialList 13 ( 14 @atrid int 15 ) 16 RETURNS TABLE 17 AS 18 RETURN 19 ( 20 select * from Material where AtrID=@atrid 21 ) 22 GO 23 24 --使用: 25 26 select * from fn_FunGetMaterialList(5)
顾名思义:表值函数返回的是表,而标量值函数可以返回基类型
- 表值函数
用户定义表值函数返回 table 数据类型。对于内联表值函数,没有函数主体;表是单个 SELECT 语句的结果集。
以下示例创建了一个内联表值函数。此函数的输入参数为客户(商店)ID,而返回 ProductID
、Name
以及 YTD Total
(销售到商店的每种产品的本年度节截止到现在的销售总额)列。
1 USE AdventureWorks;
2 GO
3 CREATE FUNCTION Sales.fn_SalesByStore (@storeid int)
4 RETURNS TABLE
5 AS
6 RETURN
7 (
8 SELECT P.ProductID, P.Name, SUM(SD.LineTotal) AS 'YTD Total'
9 FROM Production.Product AS P
10 JOIN Sales.SalesOrderDetail AS SD ON SD.ProductID = P.ProductID
11 JOIN Sales.SalesOrderHeader AS SH ON SH.SalesOrderID = SD.SalesOrderID
12 WHERE SH.CustomerID = @storeid
13 GROUP BY P.ProductID, P.Name
14 );
15 GO
1 --以下示例调用此函数并指定客户 ID 为 602
2
3
4 SELECT * FROM Sales.fn_SalesByStore (602);
对于多语句表值函数,在 BEGIN...END 语句块中定义的函数体包含一系列 Transact-SQL 语句,这些语句可生成行并将其插入将返回的表中。
以下示例创建了一个表值函数。此函数具有一个输入参数 EmployeeID
而返回直接或间接向指定员工报告的所有员工的列表。
1 USE AdventureWorks; 2 GO 3 CREATE FUNCTION dbo.fn_FindReports (@InEmpID INTEGER) 4 RETURNS @retFindReports TABLE 5 ( 6 EmployeeID int primary key NOT NULL, 7 Name nvarchar(255) NOT NULL, 8 Title nvarchar(50) NOT NULL, 9 EmployeeLevel int NOT NULL, 10 Sort nvarchar (255) NOT NULL 11 ) 12 --Returns a result set that lists all the employees who report to the 13 --specific employee directly or indirectly.*/ 14 AS 15 BEGIN 16 WITH DirectReports(Name, Title, EmployeeID, EmployeeLevel, Sort) AS 17 (SELECT CONVERT(Varchar(255), c.FirstName + ' ' + c.LastName), 18 e.Title, 19 e.EmployeeID, 20 1, 21 CONVERT(Varchar(255), c.FirstName + ' ' + c.LastName) 22 FROM HumanResources.Employee AS e 23 JOIN Person.Contact AS c ON e.ContactID = c.ContactID 24 WHERE e.EmployeeID = @InEmpID 25 UNION ALL 26 SELECT CONVERT(Varchar(255), REPLICATE ('| ' , EmployeeLevel) + 27 c.FirstName + ' ' + c.LastName), 28 e.Title, 29 e.EmployeeID, 30 EmployeeLevel + 1, 31 CONVERT (Varchar(255), RTRIM(Sort) + '| ' + FirstName + ' ' + 32 LastName) 33 FROM HumanResources.Employee as e 34 JOIN Person.Contact AS c ON e.ContactID = c.ContactID 35 JOIN DirectReports AS d ON e.ManagerID = d.EmployeeID 36 ) 37 -- copy the required columns to the result of the function 38 INSERT @retFindReports 39 SELECT EmployeeID, Name, Title, EmployeeLevel, Sort 40 FROM DirectReports 41 RETURN 42 END; 43 GO 44 --在以下示例中,调用了此函数。 45 -- Example invocation 46 SELECT EmployeeID, Name, Title, EmployeeLevel 47 FROM dbo.fn_FindReports(109) 48 ORDER BY Sort;
内联表值型函数(Inline table-valued functions)以表的形式返回一个返回值,即它返回的是一个表内联表值型函数没有由BEGIN-END 语句括起来的函数体。其返回的表由一个位于RETURN 子句中的SELECT 命令段从数据库中筛选出来。内联表值型函数功能相当于一个参数化的视图。
以下是一个例子,是将一个人事考勤排班的数据显示到Grid,中间嵌套了其他自定义函数
1 CREATE FUNCTION HR_SQL_A03 ()
2 RETURNS table
3 AS
4 return
5 (
6 Select top 100 percent
7 case isnull(status,0) when 0 then '' when 1 then '已审核' when 2 then '已审批'
8
9 when 3 then '核对' end as check_status_name ,
10 Class_No,Class_Name,Time_Work,
11 In1_Min,Time1_In,In1_Max,
12
13 dbo.get_100_name('day_type',Time1_In_Day) as Time1_In_Day_name ,
14 Out1_Min,time1_out,Out1_Max,
15
16 dbo.get_100_name('day_type',Time1_out_Day) as Time1_out_Day_name ,
17 dbo.get_100_name('work_type',Time1_Type) as time1_type_name,
18
19 other_1,time1_work,time1_rest,
20 In2_Min,Time2_In,In2_Max,
21
22 dbo.get_100_name('day_type',Time2_In_Day) as time2_in_day_name ,
23 Out2_Min,time2_out,Out2_Max,
24
25 dbo.get_100_name('day_type',Time2_out_Day) as time2_out_day_name,
26 dbo.get_100_name('work_type',Time2_Type) as time2_type_name,
27
28 Other_2,time2_work,time2_rest,
29 In3_Min,Time3_In,In3_Max,
30
31 dbo.get_100_name('day_type',Time3_In_Day) as time3_in_day_name ,
32 Out3_Min,time3_out,Out3_Max,
33
34 dbo.get_100_name('day_type',Time3_out_Day) as time3_out_day_name,
35 dbo.get_100_name('work_type',Time3_Type) as time3_type_name,
36
37 Other_3,time3_work,time3_rest,
38 in4_Min,Time4_In,In4_Max,
39
40 dbo.get_100_name('day_type',Time4_In_Day) as time4_in_day_name ,
41 Out4_Min,time4_out,Out4_Max,
42
43 dbo.get_100_name('day_type',time4_out_day) as Time4_out_Day_name,
44 dbo.get_100_name('work_type',Time4_Type) as time4_type_name,
45
46 Other_4,time4_work,time4_rest,
47 Upd_Name ,Upd_Date , check_1,date_1, check_2,date_2, check_3,date_3 ,
48 isnull(status,0) as status ,
49 Auto1_a,Auto1_b,Auto2_a,Auto2_b,Auto3_a,Auto3_b,Auto4_a,Auto4_b,
50 check1,zheng_1,check2,zheng_2,check3,zheng_3,check4,zheng_4,
51 Time1_In_Day,Time1_out_Day,Time2_In_Day, Time2_out_Day,
52 Time3_In_Day,Time3_out_Day,Time4_In_Day,Time4_out_Day,
53 Time1_Type,Time2_Type,Time3_Type,Time4_Type
54 From hrstp08
55 Order By Class_No
56 )