SQL OpenQuery with Recursive CTE

2.5k views Asked by At

I have the following Recursive CTE which is used in an OpenQuery function in SQL. I use OpenQuery to query a SQL Linked Server.

(DOES NOT WORK)

SELECT * FROM OPENQUERY([MyLinkedServerDB], 'WITH SPChainCTE (NAME, INHERIT_FROM) 
AS
(
    Select sp.NAME, sp.INHERIT_FROM from PRODUCT AS sp
    where NAME = ''ProductName''
    UNION ALL
    Select sp.NAME, sp.INHERIT_FROM from PRODUCT AS sp
    INNER JOIN SPChainCTE AS ch on sp.NAME = ch.INHERIT_FROM
)
Select NAME, INHERIT_FROM from SPChainCTE;
');

The Above CTE does not work.

However if I execute a non recursive CTE using OpenQuery it works fine. (This works)

SELECT * FROM OPENQUERY([FP], 'WITH SPChainCTE (NAME, INHERIT_FROM) 
AS
(
    Select sp.NAME, sp.INHERIT_FROM from ENG.FOCALPOINT.SW_PRODUCT AS sp
    where sp.NAME = ''ProductName''

)
Select NAME, INHERIT_FROM from SPChainCTE;
');

Any suggestions? Am I missing something here?

0

There are 0 answers