开发者

How to bulk update a table with recursive data

I have created a temporary table where I have duplicated rows with new id's in the same table. All have parent id's that has to be updated according to the old id's

A created temporary table has the fields (new_id, new_parentid, old_id, old_parentid) the new_id, old_id and old_parentid are inserted by a bulk insert.

Does anybody have an idea about how to update the New_Parent_Id so it will have the same structure as the old?

declare @STARTNODE int

declare @tempTable table(New_Id int, New_Parent_Id int, Old_Id int, Old_Parent_Id int)

INSERT ORIGTABLE(OLD_GROUPID,OLD_GROUPPARENTID)
OUTPUT inserted.New_Id, inserted.Old_Id, inserted.Old_Parent_Id 
into @tempTable(New_Id,Old_Id,Old_Parent_Id)
SELECT a.ID, a.PARENT_ID FROM SHOP_ORIGTABLE a 
WHERE a.ID IN (SELECT ID FROM fn_allGroups(@开发者_如何学GoSTARTNODE))

where the function fn_allGroups gets all fields from a hierarchy.


if I correclty understood your table's structure, after the insert on @tempTable you have to run this update:

UPDATE a
SET a.new_parent_id = b.new_id
FROM @tempTable a
    INNER JOIN @tempTable b ON b.old_id = a.old_parent_id
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜