是一种在主查询之外定义子查询的方法,以便sql代码变得更容易阅读吗?例如:
而不是:
Select *
From
Select *
From
Select *
From TableName
我想:
Select *
From A
Define A = Select * From B
Define B = Select * From TableName
我正在使用SQL Server 2008,如果这是相关的。
答案 0 :(得分:4)
请尝试这样的事情。使用公用表表达式。
;
WITH B
AS (
SELECT *
FROM TableName
)
,A
AS (
SELECT *
FROM B
)
SELECT *
FROM A;
答案 1 :(得分:0)
这也可以通过观点来完成(我认为这比CTE更符合您的要求:
CREATE VIEW B AS (Select * From TableName);
CREATE VIEW A AS (Select * From B);
SELECT *
FROM A;