使用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>
我是否正确地解决了这个问题?
答案 0 :(得分:3)
您不能在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>
答案 1 :(得分:2)
如其他人所述,您不能在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;