2013-03-29 61 views
0

如何在這樣的查詢中使用?有沒有辦法在TableList中使用tableIds?我可以把它們當作子查詢嗎?在where子句中使用sql與

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 
) 

編輯:爲了說明爲什麼我不能在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) 

我覺得這個節目爲什麼我嘗試在SQL查詢的中間有子句中使用。

+0

你想在這裏完成什麼? –

+0

即時嘗試讓表遞歸。在我的OldTable列中,像TableId,ParentTableId和Isdeleted。通過使用這個SQL即時通訊試圖獲取tableId = 5 –

回答

1

您需要調用SQL之前定義與CTE您:

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 
) 

編輯 - 根據您的意見,您可以在VIEW中定義遞歸CTE,然後根據需要使用:

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; 
+0

對我來說看起來像死衚衕。你有任何關於遞歸sql的建議嗎? –

+0

@EmreErişgen:不在SQLServer中。 –

+0

@EmreErişgen - 您可以將遞歸CTE定義爲視圖,然後相應地使用。 – sgeddes

1

我更喜歡在JOIN這樣做,所以它看起來像這樣。 WITH語句始終是sql查詢語句中的第一個語句。

DISTINCT關鍵字上SELECT子句施加,以使結果是獨特如果曾經有從TestTable具有在公共表表達式多個匹配的記錄。

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 

更新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) 
+0

的所有子表我不能使用語句首先在SQL查詢。我需要像使用子查詢一樣使用它。 –

+0

你不能使用它的原因是什麼?您不能在子查詢中包裝公用表表達式。 –

+0

我編輯的問題來顯示即時消息試圖做什麼。 –