使用 TSQL,如何通过最初在 SELECT 子句中调用它,然后在 WHERE 子句中引用结果来从用户定义的函数中获取值?我尝试为结果创建别名,但无法解析名称
SELECT C.dbo.myFunction(n.first, n.last) AS result, <other columns>
FROM C.dbo.Names AS n, <other tables>
WHERE result > 10 AND <other conditions>
我这样做对吗?
您不能在WHERE
子句中使用列别名。您可以尝试以下操作:
SELECT s.*
FROM
(SELECT C.dbo.myFunction(n.first, n.last) AS result, <other columns>
FROM C.dbo.Names AS n, <other tables>) s
WHERE s.result > 10 AND <other conditions>
正如其他人提到的,您不能在Where
子句中使用别名。但是,您可以改用公用表表达式:
With CTE AS
(SELECT C.dbo.myFunction(n.first, n.last) AS result, <other columns>
FROM C.dbo.Names AS n, <other tables> )
SELECT * FROM CTE WHERE result > 10;