2013-04-29 22 views
0

我想通過MySQL函數複製樹。我的表結構是這樣的:通過SQL函數複製樹

id|parent_id|name|position 
1|0|rootnode|1 
2|1|firstchild|1 
3|1|secondchild|2 
4|0|anotherroot|2 
5|4|anotherchild|1 

如果我複製它,它應該是這樣的(記住:id爲AUTOINC):

id|parent_id|name|position 
6|0|rootnode|1 
7|6|firstchild|1 
8|6|secondchild|2 
9|0|anotherroot|2 
10|9|anotherchild|1 

這可能嗎?在MySQL中可以使用recursiv函數嗎? MySQL的版本是5.0.95

最好的問候......從the manual

+0

希望這有助於http://stackoverflow.com/questions/15584013/how-to-find-the-hierarchy-path-for-a-tree-representation/15861254#15861254 – Meherzad 2013-04-29 08:32:20

+0

嗨,我想複製整個樹。我的問題是autoincrement id的再生 - 所以我失去了作業。 – fillibuster 2013-04-29 08:38:21

+1

你可以發佈一些例子... – Meherzad 2013-04-29 08:39:43

回答

0

摘錄:

存儲函數不能是遞歸的。

存儲過程中的遞歸是允許的,但默認情況下是禁用的。

作爲註釋的link provided by Meherzad顯示如何掃描過程中的樹。 This link將爲您提供有關如何使用IN和OUT過程參數模擬函數調用的提示。