Question

How can i use with in a query like this? Is there a way to use the tableIds in TableList? Can i treat them like a subquery?

select * from TestTable where tableId in 

(WITH TableList (tableId)
AS
(
SELECT tableId FROM OldTable WHERE tableId=5 and IsDeleted = 0
UNION ALL
SELECT t.tableId From OldTable as t
INNER JOIN TableList as p
ON t.ParenTableId = p.TableId
WHERE t.IsDeleted = 0
)
SELECT tableId FROM TableList
)

EDIT: To show why i cant use with in first statement of sql.


select TableId, IsDeleted,
( 
  select count(OtherId) OtherTable
  where otherTableId in 
  (WITH TableList (tableId)
  AS
  (
  SELECT tableId FROM OldTable WHERE tableId=5 and IsDeleted = 0
  UNION ALL
  SELECT t.tableId From OldTable as t
  INNER JOIN TableList as p
  ON t.ParenTableId = p.TableId
  WHERE t.IsDeleted = 0
  )
  SELECT tableId FROM TableList
  )
  ) as Ex1
from TestTable where tableId in (1,2,3)

I think this show why im trying to use with clause in the middle of a sql query.

Was it helpful?

Solution

You need to define your WITH CTE before calling the SQL:

WITH TableList (tableId)
AS
(
    SELECT tableId FROM OldTable WHERE tableId=5 and IsDeleted = 0
    UNION ALL
    SELECT t.tableId From OldTable as t
        INNER JOIN TableList as p
            ON t.ParenTableId = p.TableId
    WHERE t.IsDeleted = 0
)
SELECT * 
FROM TestTable 
WHERE tableId in 
(
    SELECT tableId FROM TableList
)

EDIT -- Based on your comments, you could define your Recursive CTE in a VIEW and then use as needed:

CREATE VIEW YourView AS 
WITH TableList (tableId)
AS
(
    SELECT tableId FROM OldTable WHERE tableId=5 and IsDeleted = 0
    UNION ALL
    SELECT t.tableId From OldTable as t
        INNER JOIN TableList as p
            ON t.ParenTableId = p.TableId
    WHERE t.IsDeleted = 0
)
SELECT * 
FROM TableList;

OTHER TIPS

I prefer of doing this in JOIN, so it looks like this. WITH statement is always the first in the sql query statement.

A DISTINCT keyword is applied on the SELECT clause in order for the result to be unique if ever there are records from TestTable that has multiple matches on the common table expression.

WITH TableList (tableId)
AS
(
    SELECT tableId FROM OldTable WHERE tableId=5 and IsDeleted = 0
    UNION ALL
    SELECT t.tableId From OldTable as t
    INNER JOIN TableList as p
    ON t.ParenTableId = p.TableId
    WHERE t.IsDeleted = 0
)
SELECT  DISTINCT a.*
FROM    TestTable a
        INNER JOIN TableList b
            ON a.tableId = b.tableId

UPDATE 1

WITH TableList (tableId)
AS
(
    SELECT tableId FROM OldTable WHERE tableId=5 and IsDeleted = 0
    UNION ALL
    SELECT t.tableId From OldTable as t
    INNER JOIN TableList as p
    ON t.ParenTableId = p.TableId
    WHERE t.IsDeleted = 0
)
SELECT  DISTINCT t.TableId, t.IsDeleted, f.totalCount
FROM    TestTable t
        INNER JOIN
        (
            SELECT  a.otherTableId, COUNT(DISTINCT a.OtherId) totalCount
            FROM    OtherTable a
                    INNER JOIN TableList b
                        ON b.tableId = a.otherTableId
            GROUP   BY otherTableId
        ) f ON t.tableId = f.otherTableId
WHERE   t.tableId in (1,2,3)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top