我創建了一個如下所示的遞歸查詢。SQL - 數據庫中已經有一個名爲'variableName'的對象
;WITH Hierachy(LevelID)
AS (SELECT LevelID
FROM tDDFLevel r
WHERE r.LevelID IN
(SELECT LevelID
FROM tDDFViewLevelMapping
WHERE ViewID = 2
)
AND r.LevelID NOT IN (SELECT ObjectID FROM tSysTrashBin)
AND r.Parent = -1 UNION ALL SELECT c.LevelID
From tDDFLevel c
INNER Join Hierachy ch ON c.Parent = ch.LevelID
And c.LevelID Not IN (SELECT ObjectID FROM tSysTrashBin)
)
該查詢的結果將始終是一列整數。我想要做的是,我希望將結果保存在整數的列表,以便我已經修改了查詢,現在看起來像:
declare @LevelIDs int
;WITH Hierachy(LevelID)
AS (SELECT LevelID
FROM tDDFLevel r
WHERE r.LevelID IN
(SELECT LevelID
FROM tDDFViewLevelMapping
WHERE ViewID = 2
)
AND r.LevelID NOT IN (SELECT ObjectID FROM tSysTrashBin)
AND r.Parent = -1
UNION ALL
SELECT c.LevelID
From tDDFLevel c
INNER Join Hierachy ch ON c.Parent = ch.LevelID
And c.LevelID Not IN (SELECT ObjectID FROM tSysTrashBin)
)
SELECT LevelID INTO LevelIDs FROM Hierachy
SELECT LevelID from LevelIDs
不過,我不斷收到這說的錯誤:
消息2714,級別16,狀態6,4號線已存在名爲在數據庫中 「LevelIDs」的對象。
我錯過了什麼?
'INTO'子句創建新表。看起來你已經有了一個名爲'LevelIDs'的表。 –
使用'INSERT INTO' –
SELECT * FROM LevelIDs。它包含哪些數據?如果每次執行SQL時都填充這個數據,那麼在這種情況下最後寫DROP語句。或者如果存在chk則drop語句。不知道這是否適用於Anchor查詢... – Aditya