想 - 我已經表結構SQL Server存儲過程(菜單系統) - 微軟SQL Server 2005的
elementid, parentid, elementtitle, sortorder 160 0 Brand New Tutorial 1 161 160 Brand New Tutorial New Step 1 168 5 Tutorial Topic 1.1 1 171 168 Tutorial Topic 1.1.1 1 172 171 Tutorial Topic 1.1.1.1 1
我需要能夠設置一個存儲過程,讓我來更新Elementid的下面, Parentid的關係。
這裏是我的正常SQL用於產生的樹:
WITH menu AS
(
SELECT parentid, elementid, elementtitle, sortorder FROM dbo.ta_tutorial_elements WHERE (elementid = @eid)
UNION ALL
SELECT e.parentid, e.elementid, e.elementtitle, e.sortorderFROM dbo.ta_tutorial_elements AS e INNER JOIN menu AS m ON e.parentid = m.elementid
)
SELECT * INTO [#tmpA] FROM menu
我認爲可能是可以使用臨時表的過度複製表,然後以某種方式使用插入的身份進入我的標準表從elementid和根父母開始...然而,在那之後,我幾乎失去了如何遞歸地處理所有parentid's,elementid與他們的關係......(在SQL Server中是否可能?)。
我看到類似以下內容:
CREATE PROCEDURE [DBO] [sp_ta_copy_tutorial_by_id] @eid BIGINT AS上 SET NOCOUNT BEGIN DECLARE @recid BIGINT
SET @recid = (SELECT IDENT_CURRENT('ta_tutorial_elements'));
WITH menu AS
(
SELECT parentid, elementid, elementtitle, sortorder, userid, createddate FROM dbo.ta_tutorial_elements WHERE (elementid = @eid)
UNION ALL
SELECT e.parentid, e.elementid, e.elementtitle, e.sortorder, e.userid, e.createddate FROM dbo.ta_tutorial_elements AS e INNER JOIN menu AS m ON e.parentid = m.elementid
)
SELECT * INTO [#tmpA] FROM menu
ALTER TABLE [#tmpA]
DROP COLUMN elementid
SELECT * INTO [#tmpB] FROM [#tmpA];
UPDATE b SET b.parentid =
CASE
WHEN b.parentid <> 0
THEN @recid
ELSE 0
END
FROM [#tmpB] as b
INSERT INTO [ta_tutorial_elements] SELECT * FROM [#tmpB]
DROP TABLE [#tmpA]
DROP TABLE [#tmpB]
END
沒有「MSSQL」這樣的東西。我想你可能是指「SQL Server」。 – 2010-07-06 20:14:34
Microsoft(MS)SQL(結構化查詢語言)MSSQL。 :) – 2010-07-06 20:23:08