将多行 Table 值函数转换为内联 SQL
converting multiline Table valued function to inline in SQL
考虑到性能问题,我想制作 multiline
Table 值函数,一个 inline
TVF。
这是 Multiline TVF
的示例代码:
CREATE FUNCTION MatchAptNumber (@AptNumberFromUser nvarchar(20))
RETURNS @MatchedData Table
(
RowNumber int null ,
PercentMatch int null
)
AS
Begin
Insert into @MatchedData(RowNumber) select dbo.Patients.Rowid from dbo.Patients where dbo.Patients.Aptnumber = @AptNumberFromUser
update @MatchedData set PercentMatch= 100
RETURN;
END;
Go
以下是我的使用方法:
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
考虑到性能问题,我想制作 multiline
Table 值函数,一个 inline
TVF。
这是 Multiline TVF
的示例代码:
CREATE FUNCTION MatchAptNumber (@AptNumberFromUser nvarchar(20))
RETURNS @MatchedData Table
(
RowNumber int null ,
PercentMatch int null
)
AS
Begin
Insert into @MatchedData(RowNumber) select dbo.Patients.Rowid from dbo.Patients where dbo.Patients.Aptnumber = @AptNumberFromUser
update @MatchedData set PercentMatch= 100
RETURN;
END;
Go
以下是我的使用方法:
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