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

T-SQL“合并"兩行,或“Rekey"所有 FK 關系

T-SQL to quot;Mergequot; two rows, or quot;Rekeyquot; all FK relationships(T-SQL“合并兩行,或“Rekey所有 FK 關系)
本文介紹了T-SQL“合并"兩行,或“Rekey"所有 FK 關系的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個生產數據庫,偶爾需要合并"單個表中的冗余行.

I have a production database where occasionally redundant rows in a single table need to be "Merged".

假設此表中的兩行具有相同的值,除了它們的 ID.

Let's assume that both rows in this table have identical values, except their IDs.

Table "PrimaryStuff"
ID | SomeValue
1  | "I have value"
2  | "I have value"
3  | "I am different"

我們還假設存在多個相關表.因為在PrimaryStuff"表中創建了重復項,所以通常在這些子表中創建的行都應該與 PrimaryStuff 表上的單個記錄相關.這些表的數量和名稱不在我的控制之下,應該在運行時動態考慮. IE:我不知道名稱甚至相關記錄的數量,因為其他人可能會編輯數據庫在我不知情的情況下.

Let's also assume that a number of related tables exist. Because duplicates were created in the "PrimaryStuff" table, often rows are created in these child tables that SHOULD all be related to a single record on the PrimaryStuff table. The number and names of these tables are not under my control and should be considered dynamically at runtime. IE: I don't know the names or even the number of related records, as other people may edit the database without my knowledge.

Table "ForeignStuff"
ID | PrimaryStuffId | LocalValue
1| 1| "I have the correct FK"
2| 1| "I have the correct FK"
3| 2| "I should get pointed to an FK of 1"

為了解決 PrimaryStuff 的第 1 行和第 2 行的重復問題,我希望所有相關的表都將它們的 FK 更改為 1,然后刪除 PrimaryStuff 的第 2 行.這應該是微不足道的,就好像 PrimaryStuff 的行一樣1 不存在,我只需將第 2 行的主鍵更新為 1,更改就會級聯出來.我不能這樣做,因為這將是 PrimaryStuff 的唯一索引中的重復鍵.

To resolve the duplication of PrimaryStuff's row 1 and 2, I wish to have ALL related tables change their FK's to 1s and then delete the PrimaryStuff's row 2. This SHOULD be trivial, as if PrimaryStuff's row 1 didn't exist, I could just update the Primary Key on Row 2 to 1, and the changes would cascade out. I cannot do this because that would be a duplicate key in the PrimaryStuff's unique index.

請隨時提出問題,我會盡力解決任何令人困惑的問題.

Feel free to ask questions and I'll try to clear up anything that's confusing.

推薦答案

首先讓我們獲取需要更新的行列表(據我所知,您希望最低 ID 替換所有較高 ID)

First lets get a list of the rows that need to be updated (as I understand it you want the lowest ID to replace all the higher IDs)

 SELECT MIN(ID) OVER (PARTITION BY SomeValue ORDER BY SomeValue, ID ASC) AS FirstID,
        ID,
        SOMEVALUE
 FROM PrimaryStuff

我們可以去掉 FirstID 和 ID 匹配的那些,這些都沒有關系

We can remove the ones where FirstID and ID match, these don't matter

SELECT FirstID, ID FROM
(
 SELECT MIN(ID) OVER (PARTITION BY SomeValue ORDER BY SomeValue, ID ASC) AS FirstID,
        ID,
        SOMEVALUE
 FROM PrimaryStuff
) T
WHERE FirstID != ID

現在我們有一個更改列表.我們可以在更新語句中使用它,將其放入臨時表(或我在下面所做的 CTE)中:

Now we have a change list. We can use this in an update statement, put it in a temp table (or a CTE as I did below):

WITH ChangeList AS
(
  SELECT FirstID, ID FROM
  (
   SELECT MIN(ID) OVER (PARTITION BY SomeValue ORDER BY SomeValue, ID ASC) AS FirstID,
        ID
   FROM PrimaryStuff
  ) T
  WHERE FirstID != ID
)
UPDATE ForeignStuff
SET PrimaryStuffId = ChangeList.FirstID
FROM ForeignStuff
JOIN ChangeList ON ForeignStuff.ID = ChangeList.ID

注意 - 代碼未經測試,可能有錯別字.

NB - Code not tested, might have typos.

這篇關于T-SQL“合并"兩行,或“Rekey"所有 FK 關系的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

SQL - Select all when filter value is empty(SQL - 過濾器值為空時全選)
How and where do I set the MAXRECURSION option?(如何以及在何處設置 MAXRECURSION 選項?)
TABLOCKX versus SERIALIZABLE(TABLOCKX 與可序列化)
TSQL Constants... Use Variable or Literal?(TSQL 常量...使用變量還是文字?)
TSQL RIGHT String function not working(TSQL RIGHT 字符串函數不起作用)
What is the comparative speed of temporary tables to physical tables in SQL?(SQL中臨時表與物理表的比較速度是多少?)
主站蜘蛛池模板: 亚洲一区二区av | 欧美精品日韩精品国产精品 | 日韩欧美三区 | 中文在线日韩 | 国产超碰人人爽人人做人人爱 | 国产免费人成xvideos视频 | aaa在线 | 成人做爰www免费看视频网站 | 精品国产网 | 伊人精品在线 | 午夜精品福利视频 | 一区二区影院 | 国产精品久久久久久妇女6080 | 日韩国产三区 | 亚洲97 | 九色 在线| 欧美激情国产精品 | 欧美性猛片aaaaaaa做受 | 欧美日韩不卡 | 国产精品国产精品 | 欧美日韩国产一区二区三区 | 国产一区二区三区免费 | 艹逼网| 免费国产网站 | 亚洲精品一区二区三区蜜桃久 | 国产高清精品在线 | 国产精品欧美一区二区三区不卡 | av大全在线 | 日本色婷婷 | 亚洲一区二区三区四区五区中文 | 国产精品久久久久久久久久久久久久 | 欧美aaaaaa| 色天天综合| 日本黄色激情视频 | 98成人网 | 日一区二区三区 | 在线一级片 | 精品一区二区视频 | 久久综合久色欧美综合狠狠 | 国产高清在线精品 | 国产a视频 |