kombsh
kombsh

Reputation: 387

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.

I want to run Nested CTE query like this

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 ...

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

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

pls help anyone to prcoeed this, I guess my need is too dynamic

Upvotes: 3

Views: 13622

Answers (4)

GarethD
GarethD

Reputation: 69789

If your system generated query uses db qualified object names you can hack this by using 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;

Upvotes: 0

MatBailie
MatBailie

Reputation: 86775

Separate your CTEs with ,s rather than nesting them.

;
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; 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.

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.

Upvotes: 6

Teis Lindemark
Teis Lindemark

Reputation: 362

;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.

Upvotes: 0

bjnr
bjnr

Reputation: 3437

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   ...

Upvotes: 8

Related Questions