久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

有沒有辦法在給定起始 id 的情況下復制相鄰列表

Is there a way to duplicate part of a adjacent list hierarchy given a starting id?(有沒有辦法在給定起始 id 的情況下復制相鄰列表層次結構的一部分?)
本文介紹了有沒有辦法在給定起始 id 的情況下復制相鄰列表層次結構的一部分?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個構成主題結構的相鄰列表層次模型

I have an adjacent list hierarchy model that makes up a topic structure

   ID   Parent_Id Topic_Name
   1    Null      Topic 1
   2    Null      Topic 2
   3    2            Topic 3
   4    3               Topic 4
   5    2            Topic 5
   6    Null      Topic 6

這構成了我無法更改的應用程序的一部分 - 主題沒有多個父級,所以很遺憾我無法移動到嵌套集 - 盡管如果這是過程中的一個臨時步驟 - 只要它返回到相鄰列表層次模型

This forms part of an application which I cant change - the topics dont have multiple parents so unfortunatly I can't move to a nested sets - although if this was an interim step in the process - this would be fine as long as it went back to adjacent list hierarchy model

我想指定一個主題 id,然后將其復制到一個新的主題 id 并保留下面的級別/結構

I want to specify a topic id and then copy it to a new topic id and retain the levels / structure underneath

因此在我的示例中,我可以指定主題 topic_id 2 并且它會創建

So in my example I could specify topic topic_id 2 and it would create

   ID   Parent_Id Topic_Name
   7    Null      Topic 2
   8    7            Topic 3
   9    8               Topic 4
   10   7            Topic 5

ID 會自動編號,因此無需構建,但顯然需要保留父 ID

Auto numbering is taken care of for the ID so no need to construct that, but obviously the parent id needs to be retained

我怎樣才能實現上述目標?我是否需要展平數據并在每次插入后執行 3 個單獨的插入來記錄 ID?

How can I achieve the above? would I need to flatten the data and do 3 seperate inserts logging the id after each insert?

推薦答案

您可以使用遞歸 CTE 來獲取要插入的行.如果您使用 merge 添加行,您可以使用 output 來捕獲 生成ID和舊ID之間的映射,可用于更新列Parent_ID對于插入的行.

You can use a recursive CTE to get the rows to insert. If you use merge to add the rows you can use output to capture a mapping between the generated ID and the old ID which can be used to update the column Parent_ID for the inserted rows.

-- ID for topic to copy 
declare @ID int;
set @ID = 2;

-- Table to hold the inserted rows
declare @T table
(
  New_ID int,
  Old_ID int,
  Old_ParentID int
);

-- Add rows from recursive CTE using merge
with C as
(
  select T.ID, T.Parent_Id, T.Topic_Name
  from YourTable as T
  where T.ID = @ID
  union all
  select T.ID, T.Parent_Id, T.Topic_Name
  from YourTable as T
    inner join C 
      on C.ID = T.Parent_Id
)
merge YourTable
using C
on 0 = 1
when not matched then
  insert (Topic_Name) values (C.Topic_Name)
output inserted.ID,
       C.ID,
       C.Parent_Id
  into @T(New_ID, Old_ID, Old_ParentID);

-- Update Parent_Id for the new rows
update Y set
  Parent_Id = T2.New_ID
from @T as T1
  inner join @T as T2
    on T1.Old_ParentID = T2.Old_ID
  inner join YourTable as Y
    on T1.New_ID = Y.ID;

SE-Data

這篇關于有沒有辦法在給定起始 id 的情況下復制相鄰列表層次結構的一部分?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 97超碰人人草 | 亚洲欧美日韩一区二区 | 丁香六月伊人 | 国产分类视频 | 亚洲视频二区 | 亚洲午夜在线 | 亚洲一区二区综合 | 成人一区二区三区在线观看 | 欧美成人激情 | 国产日韩久久 | 亚洲精品一区二区久 | 国产精品久久久久久久久久久久久 | 自拍偷拍亚洲欧美 | 日屁视频 | 精品国产亚洲一区二区三区大结局 | 天天亚洲| 欧美亚洲成人网 | 91中文字幕在线观看 | 精品一区二区三区免费毛片 | 国产91av视频在线观看 | 有码一区 | 国产综合av| 免费视频成人国产精品网站 | 久久不卡区 | 国产不卡在线观看 | 亚洲国产高清高潮精品美女 | 狠狠久| 精品区一区二区 | 99精品视频一区二区三区 | 麻豆hd| 国产一区二区在线免费观看 | 九九亚洲精品 | 成人午夜 | 国产精品久久久久久久久久 | 国产精品伦理一区 | 天天干,夜夜操 | 午夜精品一区二区三区在线观看 | 成人午夜免费视频 | 久久精品国产99国产精品亚洲 | www九色| 国产欧美在线 |