简体   繁体   English

SQL Server中CTE内部的CTE

[英]CTE inside CTE in SQL Server

Please don't mark this question as duplicate of CTE within a CTE .. I checked that question and answer ... but that answer does not satisfy my need. 请不要在CTE中将此问题标记为CTE的副本。我检查了该问题和答案......但该答案不能满足我的需要。

I want to run Nested CTE query like this 我想像这样运行嵌套CTE查询

Drop Table #Temp
Create Table #Temp(name1 text, name2 text)

Insert INTO #Temp Values ('test','test')
Insert INTO #Temp Values ('test','test')

;WITH CTE1 AS (
   With CTE2 as ( Select * from #Temp)
)

Select * from CTE1

or 要么

;WITH CTE1 AS (
   Select * From (With CTE2 as ( Select * from #Temp))
)

Select * from CTE1

In our structure... the inner CTE2 query have been provided by other system .. so I can't control inner part of the query... so.. here my duty is only select values from inner query and form new CTE in my system ... 在我们的结构...内部CTE2查询已由其他系统提供..所以我无法控制查询的内部部分...所以..这里我的职责是只从内部查询中选择值并形成新的CTE in我的系统......

And please imagine this 请想象一下

;WITH CTE1 AS (
       "Query Provide by Other System"
    )

In some cases the "Query Provide by Other System" start with CTE..this may or may not be the CTE query... that is the exact problem for I can't use like below 在某些情况下,“由其他系统提供查询”从CTE开始......这可能是也可能不是CTE查询...这是我无法使用的确切问题,如下所示

;WITH CTE1 AS (
   Select * From 
)
,With CTE2 as
 ( Select * from #Temp))

pls help anyone to prcoeed this, I guess my need is too dynamic 请帮助任何人修改这个,我想我的需求太过动态了

Just to have an idea: 只是想一个主意:

;WITH cte1 AS
(
    SELECT * FROM ...
),
cte2 as
(
    SELECT * FROM ...
),
cte3 as
(
    SELECT * FROM ... INNER JOIN cte2 ON...
),
SELECT *
FROM
    cte1
    INNER JOIN cte3 ON   ...

Separate your CTEs with , s rather than nesting them. 用你的CTE分隔,而不是嵌套它们。

;
WITH
  CTE2 AS
(
  SELECT * FROM #Temp
)
,
  CTE1 AS
(
  SELECT * FROM CTE2
)

SELECT
  *
FROM
  CTE1


EDIT : Following your additional comments 编辑:关注您的其他评论

As I understand it, you are being provided with a system generated query that you then want to embed in another query. 据我所知,您将获得一个系统生成的查询,然后您希望将其嵌入另一个查询中。 Sometimes that system generated query uses a CTE, sometimes it doesn't; 有时系统生成的查询使用CTE,有时它不会; you don't know in advance the format of that query. 您事先并不知道该查询的格式。

Unfortunately for you this means that you can not embed this within another CTE. 不幸的是,这意味着您无法将其嵌入到另一个CTE中。

One option could be to use real views. 一种选择可能是使用真实的观点。

CREATE VIEW xxx AS
  <system generated code here>
;

SELECT
  *
FROM
  xxx
;

You do then, however, have to be very careful about concurrency; 但是,你必须非常小心并发性; two concurrent users trying to create the same view with the same name. 两个并发用户尝试使用相同的名称创建相同的视图。

The better solution would be to approach the vendor of the system with is creating the system generated query and ask them how they propose you use it. 更好的解决方案是通过创建系统生成的查询来询问系统供应商,并询问他们建议您如何使用它。

;with BASE AS (
    SELECT * FROM table1
), BASE2 AS (
   SELECT * from table2
), BASE3 AS (
    SELECT * FROM table3
) SELECT * FROM BASE INNER JOIN BASE3 ...

I guess this is what you are trying to do. 我想这就是你要做的。

If your system generated query uses db qualified object names you can hack this by using OPENQUERY : 如果您的系统生成的查询使用db限定的对象名称,您可以通过使用OPENQUERY来破解它:

WITH CTE AS
(   SELECT  *
    FROM    OPENQUERY([Your Server], 'Query Provide by Other System')
) 
SELECT  *
FROM    CTE; 

You may need to configure your server for data access: 您可能需要配置服务器以进行数据访问:

EXEC sp_serveroption 'your server', 'DATA ACCESS', TRUE;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM