问题描述
我需要传递在前一列中选择的函数的返回值,并将其作为参数传递给同一行中的后续函数。我不能使用别名:
我想要的是:
SELECT
dbo.GetSecondID(f.ID)作为SecondID,
dbo.GetThirdID(SecondID)作为ThirdID
FROM Foo f
任何解决方法?谢谢!
编辑:
方法 dbo.GetSecondID()
非常重,我正在处理表中的几百万条记录。将方法作为参数传递是不明智的。
宾果!应用 CROSS APPLY
的秘诀。以下代码很有用:
$ b $ pre $ SELECT
sndID.SecondID,
dbo.GetThirdID(sndID.SecondID) as ThirdID
FROM Foo f
CROSS APPLY
(
SELECT dbo.GetSecondID(f.ID)as SecondID
)sndID
$ c $编辑:
这只适用于 SecondID
是唯一的(只返回一条记录)或 GROUP BY
被使用
I Need to pass the return value of a function that is selected in previous column and pass it as parameter to a following function in the same row. I cannot use the alias:
What I want to have is:
SELECT
dbo.GetSecondID(f.ID) as SecondID,
dbo.GetThirdID(SecondID) as ThirdID
FROM Foo f
Any workaround? Thank you!
EDIT:
The method dbo.GetSecondID()
is very heavy and I am dealing with a couple of million records in the table. It is not wise to pass the method as a parameter.
Bingo! The secret stand in applying a CROSS APPLY
. The following code was helpful
SELECT
sndID.SecondID,
dbo.GetThirdID(sndID.SecondID) as ThirdID
FROM Foo f
CROSS APPLY
(
SELECT dbo.GetSecondID(f.ID) as SecondID
) sndID
EDIT:
This only works if SecondID
is unique (only one record is returned) or GROUP BY
is used
这篇关于将函数返回传递给同一行中的另一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!