我在 Multi-Step Table 值函数中有如下所示的 sql。我将查询缩短了一点以使其易于理解。
CREATE FUNCTION [dbo].[fn_ph]
(
-- Add the parameters for the function here
@pAsOfDate date,
@pAccountId nvarchar(15)
)
RETURNS @HH TABLE
(
-- Add the column definitions for the TABLE variable here
AsOfDate date,
AccountId nvarchar(15),
LongName varchar(100),
ShortName varchar(100)
)
AS
BEGIN
declare @longOrShortIndicator int
select @longOrShortIndicator = COUNT(distinct LongOrShortIndicator) from table1 a
select a.*,
case
when a.SecType='CASH' and @longOrShortIndicator > 1 then 'C'
else a.LongOrShortIndicator
end as LongOrShortIndicator
from Table1 a
END
表达when a.SecType='CASH' and @longOrShortIndicator > 1 then 'C'
是瓶颈。
如果我删除@longOrShortIndicator > 1
查询运行速度快
如果我在函数之外运行查询,它会快速返回......
为什么局部变量会减慢整个查询的速度?任何帮助,将不胜感激。
谢谢