在SQL中将多行表值函数转换为内联函数

在SQL中将多行表值函数转换为内联函数,sql,ssms,Sql,Ssms,考虑到性能问题,我想让我的多行表值函数成为内联TVF 以下是多行TVF的示例代码: 以下是我如何使用它: select @constVal = FunctionWeight from dbo.FunctionWeights where FunctionWeights.FunctionName = 'MatchAptNumber'; INSERT INTO #Temp2(RowNumber, ValFromFunc, FuncWeight, percentage) SELECT RowNumbe

考虑到性能问题,我想让我的多行表值函数成为内联TVF

以下是多行TVF的示例代码:

以下是我如何使用它:

select @constVal = FunctionWeight from dbo.FunctionWeights where FunctionWeights.FunctionName = 'MatchAptNumber';

INSERT INTO #Temp2(RowNumber, ValFromFunc, FuncWeight, percentage)
SELECT RowNumber, PercentMatch, @constVal, PercentMatch * @constVal
from dbo.MatchAptNumber(@Aptnumber);

是否可以将其转换为内联TVF并如上所述使用?我确实知道两者在句法上的差异,但不确定如何才能用同样的方式来使用它?我可以在同一个指针上获取一些指针吗?

您可以在SELECT中获取“100”作为常量,使函数变为

CREATE FUNCTION MatchAptNumber (@AptNumberFromUser nvarchar(20))
    RETURNS TABLE AS RETURN
    SELECT 
        p.Rowid AS RowNumber , 
        CAST(100 AS INT) AS PercentMatch 
    FROM 
        dbo.Patients p
    WHERE 
        p.Aptnumber = @AptNumberFromUser
GO
CREATE FUNCTION MatchAptNumber (@AptNumberFromUser nvarchar(20))
    RETURNS TABLE AS RETURN
    SELECT 
        p.Rowid AS RowNumber , 
        CAST(100 AS INT) AS PercentMatch 
    FROM 
        dbo.Patients p
    WHERE 
        p.Aptnumber = @AptNumberFromUser
GO