定義
對于SQL Server來講,我們聲明一個變量的方式是用@變量名,而且相對于編程來講,SQL Server聲明的方式跟我們開了個玩笑,是先變量后面才是類型。對于需要傳參跟不需要傳參的方式,其實跟我們編程的方式一樣。有參數則是如下方式:
CREATE FUNCTION GetSum
(
@firstNum int,
@secondNum int
)
如果沒有參數,則只要保留括號即可。跟我們理解的函數寫法一致。
CREATE FUNCTION GetSum
(
)
標量函數
所謂標量函數簡單點來講就是返回的結果只是一個標量,對于我來講,返回的結果就是一種類型的一個值。
寫法如下:
CREATE FUNCTION <Scalar_Function_Name, sysname, FunctionName>
(
-- Add the parameters for the function here
<@Param1, sysname, @p1> <Data_Type_For_Param1, , int>
)
RETURNS <Function_Data_Type, ,int>
AS
BEGIN
-- Declare the return variable here
DECLARE <@ResultVar, sysname, @Result> <Function_Data_Type, ,int>
-- Add the T-SQL statements to compute the return value here
SELECT <@ResultVar, sysname, @Result> = <@Param1, sysname, @p1>
-- Return the result of the function
RETURN <@ResultVar, sysname, @Result>
END
例子:
CREATE FUNCTION GetSum
(
@firstNum int,
@secondNum int
)
RETURNS int
AS
BEGIN
-- Declare the return variable here
DECLARE @result int
-- Add the T-SQL statements to compute the return value here
SELECT @result=@firstNum+@secondNum
-- Return the result of the function
RETURN @result
END
GO
內聯表值函數
內聯表值函數返回的是表數據,表數據就是Table類型。
寫法如下:
CREATE FUNCTION <Inline_Function_Name, sysname, FunctionName>
(
-- Add the parameters for the function here
<@param1, sysname, @p1> <Data_Type_For_Param1, , int>,
<@param2, sysname, @p2> <Data_Type_For_Param2, , char>
)
RETURNS TABLE
AS
RETURN
(
-- Add the SELECT statement with parameter references here
SELECT 0
)
GO
例子:
CREATE FUNCTION selectTeacherTest
(
@Name varchar(20)
)
RETURNS TABLE
AS
RETURN
(
select * from Teacher where Teacher.Name=@Name
)
GO
調用: 必須從返回表里面進行“查詢”
select * from selectTeacherTest('劉英')
多語句表值函數
多語句表值函數跟內聯表值函數都是表值函數,它們返回的結果都是Table類型。多語句表值函數顧名思義,就是可以通過多條語句來創建Table類型的數據。這里不同于內聯表值函數,內聯表值函數的返回結果是由函數體內的SELECT語句來決定。而多語句表值函數,則是需要指定具體的Table類型的結構。也就是說返回的Table,已經定義好要哪些字段返回。所以它能夠支持多條語句的執行來創建Table數據。
寫法如下:
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE FUNCTION <Table_Function_Name, sysname, FunctionName>
(
-- Add the parameters for the function here
<@param1, sysname, @p1> <data_type_for_param1, , int>,
<@param2, sysname, @p2> <data_type_for_param2, , char>
)
RETURNS
<@Table_Variable_Name, sysname, @Table_Var> TABLE
(
-- Add the column definitions for the TABLE variable here
<Column_1, sysname, c1> <Data_Type_For_Column1, , int>,
<Column_2, sysname, c2> <Data_Type_For_Column2, , int>
)
AS
BEGIN
-- Fill the table variable with the rows for your result set
RETURN
END
GO
例子:
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER FUNCTION DemoFun
(
)
RETURNS
@result TABLE
(
name nvarchar(20),
city nvarchar(20),
age int,
salary int
)
AS
BEGIN
-- Fill the table variable with the rows for your result set
insert into @result(name, city, age, salary)
select FName,FCity,FAge,FSalary from dbo.T_Person where FSalary>8000
insert into @result(name, city, age, salary) values
('測試','China', 1, 0)
RETURN
END
GO
其他
可以看得出,多語句表值函數的返回結果是定義好表結構的虛擬表。這又跟標量函數一樣了吧,只不過標量函數是返回一種類型的標量值而已。而且在多語句表值函數里面,你也會發現最后一句是RETURN。告訴執行程序,多語句表值函數已經執行完成。函數體結構跟標量函數的結構一樣。對于類型放在變量后面這種方式確實需要好好轉換一下觀念。
RETURNS
<@Table_Variable_Name, sysname, @Table_Var> TABLE
(
-- Add the column definitions for the TABLE variable here
<Column_1, sysname, c1> <Data_Type_For_Column1, , int>,
<Column_2, sysname, c2> <Data_Type_For_Column2, , int>
)