2010-07-06 147 views
1

想 - 我已經表結構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

+0

沒有「MSSQL」這樣的東西。我想你可能是指「SQL Server」。 – 2010-07-06 20:14:34

+0

Microsoft(MS)SQL(結構化查詢語言)MSSQL。 :) – 2010-07-06 20:23:08

回答

0
 
CREATE PROCEDURE [dbo].[sp_ta_copy_tutorial_by_id] 
    @eid bigint 
AS 
SET nocount on 
BEGIN 
/*************************************** 
*  CREATE A 
***************************************/ 
SELECT   * 
INTO  #tmpA 
FROM  ta_tutorial_elements 
WHERE  elementid IN (SELECT elementid from fn_ta_tutorial_tree_by_element (@EID)) 

/*************************************** 
*  DUPLICATE RECORDS 
***************************************/ 
DECLARE   @CNT INT 
SET    @CNT = (SELECT max(elementid) FROM ta_tutorial_elements) 

INSERT    
INTO  ta_tutorial_elements (elementtitle, parentid) 
      SELECT elementtitle, parentid FROM #tmpA 

/*************************************** 
*  CREATE B 
***************************************/ 
SELECT   * 
INTO  #tmpB 
FROM  ta_tutorial_elements 
WHERE  elementid > @CNT 

SELECT   bpid.elementid as originalelementid, 
        brow.elementid as newparentid 
INTO  #tmpC 
FROM  #tmpB bpid LEFT OUTER JOIN 
        #tmpA aeid ON bpid.parentid = aeid.elementid LEFT OUTER JOIN 
        (
         SELECT   elementid, ROW_NUMBER() OVER (ORDER BY elementid ASC) as rownum 
         FROM  #tmpA 
       ) arow ON arow.elementid = aeid.elementid LEFT OUTER JOIN 
        (
         SELECT   elementid, ROW_NUMBER() OVER (ORDER BY elementid ASC) as rownum 
         FROM  #tmpB 
       ) brow ON brow.rownum = arow.rownum LEFT OUTER JOIN 
        #tmpB beid ON beid.elementid = brow.elementid 

UPDATE   #tmpC 
SET    newparentid = 0 
WHERE  newparentid IS NULL 

UPDATE   t2 
SET    parentid = t1.newparentid 
FROM  #tmpC t1 LEFT OUTER JOIN 
        ta_tutorial_elements t2 ON t1.originalelementid = t2.elementid 

/*************************************** 
*  TEMP DISPLAY 
***************************************/ 
SELECT * FROM ta_tutorial_elements WHERE elementid > @CNT 

DROP TABLE #tmpA 
DROP TABLE #tmpB 
DROP TABLE #tmpC 

END 
1

結賬CTEs (common table expressions)。您可以在存儲過程的上下文中使用CTE在某些遞歸之後生成菜單數據。其實它看起來像你已經有一個CTE;該鏈接描述了該上下文中的遞歸。

+0

是的,我熟悉CTE並且使用它們有點舒服...我現在剛剛失去了在哪裏更新parentid's,以及elementid是爲我的整個樹... ie: – 2010-07-06 20:28:09