在 sql 中用过程执行替换函数调用
Replacing function call with procedure execution in sql
我有一个名为 dbo.Match
的函数。它是一个 inline TVF
,我将其替换为一个名为 dbo.Match
的过程,该过程在其末尾有一个 select 语句 select 行来自 table 以便我可以在执行 dbo.Match
时将 select 查询的结果定向到名为 #Temp
的临时 table。
现在,如果它是一个函数,我正在使用这个查询:
if @MotherFN is not null
begin
SELECT @constVal = FunctionWeight
FROM dbo.FunctionWeights
WHERE FunctionWeights.FunctionId = 20;
INSERT INTO #Temp2
(RowNumber,ValFromUser,ColumnName,ValFromFunc,FuncWeight,percentage)
SELECT RowId,
@MotherFN ,
'mothersfirstname'
,PercentMatch,
@constVal,
PercentMatch * @constVal
FROM dbo.Match(@MotherFN)
end
现在,我需要执行 dbo.Match
过程而不是 dbo.Match
function.How 我可能会调用此执行并在 #Temp
table 中插入数据,例如我在做函数调用 ?
问题: 在同一步骤中有效地计算 PercentMatch * @constVal
并插入 #Temp
。程序 dbo.Match 只会 return rowId
和 PercentMatch
。我需要在 #Temp
中插入 RowId 和 PercentMatch 的值以及 @constVal
的值以及 PercentMatch
和 @constval
的乘法结果值
您的选择受到程序的更多限制。
您可以使用 insert into ... exec ...
将过程的结果插入到一个临时的 table 中,但您不能真正将它与另一个查询结合起来。 (好吧,您可以将 openrowset
与动态 SQL 一起使用,但这很快就会变得很糟糕。
例如:
if @MotherFN is not null
begin
select
@constVal = FunctionWeight
from
dbo.FunctionWeights
where
FunctionWeights.FunctionId = 20;
insert into #Temp2 (
RowId, ColumnName, ValFromFunc
) exec
dbo.Match(@MotherFN);
update
#Temp2
set
ValFromUser = @MotherFN,
FuncWeight = @constVal,
percentage = PercentMatch * @constVal;
end;
我会让程序接受以下这些参数
@MotherFN , @constVal
并在过程中执行以下操作,在 select 语句中 returns 过程的结果集。
SELECT RowId,
@MotherFN , --<-- In proc definition
'mothersfirstname'
,PercentMatch,
@constVal, --<-- In proc definition
PercentMatch * @constVal --<-- In proc definition
对于插入,只需执行
INSERT INTO #TemP (RowNumber,ValFromUser,ColumnName
,ValFromFunc,FuncWeight,percentage)
Exec dbo.Match(@MotherFN , @constVal)
我有一个名为 dbo.Match
的函数。它是一个 inline TVF
,我将其替换为一个名为 dbo.Match
的过程,该过程在其末尾有一个 select 语句 select 行来自 table 以便我可以在执行 dbo.Match
时将 select 查询的结果定向到名为 #Temp
的临时 table。
现在,如果它是一个函数,我正在使用这个查询:
if @MotherFN is not null
begin
SELECT @constVal = FunctionWeight
FROM dbo.FunctionWeights
WHERE FunctionWeights.FunctionId = 20;
INSERT INTO #Temp2
(RowNumber,ValFromUser,ColumnName,ValFromFunc,FuncWeight,percentage)
SELECT RowId,
@MotherFN ,
'mothersfirstname'
,PercentMatch,
@constVal,
PercentMatch * @constVal
FROM dbo.Match(@MotherFN)
end
现在,我需要执行 dbo.Match
过程而不是 dbo.Match
function.How 我可能会调用此执行并在 #Temp
table 中插入数据,例如我在做函数调用 ?
问题: 在同一步骤中有效地计算 PercentMatch * @constVal
并插入 #Temp
。程序 dbo.Match 只会 return rowId
和 PercentMatch
。我需要在 #Temp
中插入 RowId 和 PercentMatch 的值以及 @constVal
的值以及 PercentMatch
和 @constval
您的选择受到程序的更多限制。
您可以使用 insert into ... exec ...
将过程的结果插入到一个临时的 table 中,但您不能真正将它与另一个查询结合起来。 (好吧,您可以将 openrowset
与动态 SQL 一起使用,但这很快就会变得很糟糕。
例如:
if @MotherFN is not null
begin
select
@constVal = FunctionWeight
from
dbo.FunctionWeights
where
FunctionWeights.FunctionId = 20;
insert into #Temp2 (
RowId, ColumnName, ValFromFunc
) exec
dbo.Match(@MotherFN);
update
#Temp2
set
ValFromUser = @MotherFN,
FuncWeight = @constVal,
percentage = PercentMatch * @constVal;
end;
我会让程序接受以下这些参数
@MotherFN , @constVal
并在过程中执行以下操作,在 select 语句中 returns 过程的结果集。
SELECT RowId,
@MotherFN , --<-- In proc definition
'mothersfirstname'
,PercentMatch,
@constVal, --<-- In proc definition
PercentMatch * @constVal --<-- In proc definition
对于插入,只需执行
INSERT INTO #TemP (RowNumber,ValFromUser,ColumnName
,ValFromFunc,FuncWeight,percentage)
Exec dbo.Match(@MotherFN , @constVal)