我们如何在 sql server 的子查询中使用 CTE?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1914151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How we can use CTE in subquery in sql server?
提问by Paresh
How we can use CTE in subquery in sql server?
我们如何在 sql server 的子查询中使用 CTE?
like ..
喜欢 ..
select id (i want to use CTE here), name from table_name
选择 id(我想在这里使用 CTE),名称来自 table_name
回答by Maximilian Mayerl
Just define your CTE on top and access it in the subquery?
只需在顶部定义您的 CTE 并在子查询中访问它?
WITH YourCTE(blubb) AS
(
SELECT 'Blubb'
)
SELECT id,
(SELECT blubb FROM YourCTE),
name
FROM table_name
回答by Alfred E. Mustermann
It doesn't work:
它不起作用:
select id (I want to use CTE here), name from table_name
It's not possible to use CTE in sub queries.
不能在子查询中使用 CTE。
You can realize it as a work around:
您可以将其视为一种解决方法:
CREATE VIEW MyCTEView AS ..here comes your CTE-Statement.
Then you are able to do this:
然后你可以这样做:
select id (select id from MyCTEView), name from table_name