我有一个Temp表,我从存储过程填充。一旦数据在临时表中,我就用它来运行计算并填充其他字段。
示例:
CREATE TABLE #Result1 (
SOrder integer,
Var1 double,
Var2 double,
Var3 double,
Calc1 double,
Calc2 double )
--Fills the Temp Table initially
Insert #Result1
(Select sorder, var1, var2, var3, 0,0,0 from myTable)
--Fills with calculation stuff
Select SOrder = 0,
Calc1 = Var1/Var2 * 100
Calc2 = (Var2-Var1)/Var3 * 100
From #Result1
我希望能够做到的是:
--Fills with SP stuff
Select SOrder = 0,
Calc1 = exec spLocal_CalcMetric (Var1, Var2)
Calc2 = exec spLocal_CalcMetric (Var1, Var2)
From #Result1
有没有办法做这样的事情来更新临时表和SP的回复?
答案 0 :(得分:2)
你可以使用function
,而不是procedure
来完成,但你所做的只是简单的选择。功能也可能是性能问题。但是,您可以这样做:
CREATE FUNCTION dbo.Local_CalcMetric
(
@var1 DOUBLE,
@var2 DOUBLE,
@var3 DOUBLE
)
RETURNS DOUBLE
AS
BEGIN
RETURN (@var2-@var1)/@var3 * 100
END
并使用它:
SELECT SOrder = 0,
Calc1 = dbo.Local_CalcMetric (Var1, Var2, Var3)
Calc2 = dbo.Local_CalcMetric (Var1, Var2, Var3)
FROM #Result1