我有點麻煩讓MSSQL與我的意圖合作。SQL Server更新加入兩個表
檢查以下...
重複
UseId | Use ------------- 2254 | Primary 2255 | Primary 2256 | Primary 2257 | Intersection 2258 | Monitor 2259 | Primary 2260 | Intersection 2261 | Primary 2264 | Monitor 2269 | Secondary
的唯一身份
UseID | Use -------------- 2252 | Intersection 2253 | Monitor 2251 | Primary 2262 | Secondary
多對多表
ID | FKey | UseId ------------------- 1 | 1 | 2251 2 | 1 | 2252 3 | 2 | 2253 4 | 2 | 2262 5 | 3 | 2254 6 | 3 | 2257 7 | 3 | 2258 8 | 4 | 2255 9 | 4 | 2260 10 | 5 | 2259 11 | 5 | 2269
我想運行在更新查詢「多對多」表將與重複項和唯一標識符上的使用列相匹配,然後在多對多表中使用其唯一的UseId替換並「複製」UseId ...
生成的良好數據看起來像這樣... 。
好多對多表...
ID | FKey | UseId ------------------- 1 | 1 | 2251 2 | 1 | 2252 3 | 2 | 2253 4 | 2 | 2262 5 | 3 | 2251 6 | 3 | 2252 7 | 3 | 2253 8 | 4 | 2251 9 | 4 | 2262 10 | 5 | 2251 11 | 5 | 2262
任何幫助表示讚賞
爲響應張貼一個答案......我在索姆困難。它似乎沒有工作。
update命令正在更改Many to many表中的記錄1-4(即使它們很好)...並且不更改任何其他記錄。
此腳本提供了一個問題的例子。
CREATE TABLE Uniques
(
UseID INT,
[Use] VARCHAR(50)
)
INSERT INTO Uniques (UseID, [Use]) VALUES (2254, 'Primary')
INSERT INTO Uniques (UseID, [Use]) VALUES (2255, 'Primary')
INSERT INTO Uniques (UseID, [Use]) VALUES (2256, 'Primary')
INSERT INTO Uniques (UseID, [Use]) VALUES (2257, 'Intersection')
INSERT INTO Uniques (UseID, [Use]) VALUES (2258, 'Monitor')
INSERT INTO Uniques (UseID, [Use]) VALUES (2259, 'Primary')
INSERT INTO Uniques (UseID, [Use]) VALUES (2260, 'Intersection')
INSERT INTO Uniques (UseID, [Use]) VALUES (2261, 'Primary')
INSERT INTO Uniques (UseID, [Use]) VALUES (2264, 'Monitor')
INSERT INTO Uniques (UseID, [Use]) VALUES (2269, 'Secondary')
CREATE TABLE Duplicates
(
UseID INT,
[Use] VARCHAR(50)
)
INSERT INTO Duplicates (UseID, [Use]) VALUES (2252, 'Intersection')
INSERT INTO Duplicates (UseID, [Use]) VALUES (2253, 'Monitor')
INSERT INTO Duplicates (UseID, [Use]) VALUES (2251, 'Primary')
INSERT INTO Duplicates (UseID, [Use]) VALUES (2262, 'Secondary')
CREATE TABLE ManyToMany
(
Id INT,
FKey INT,
UseId INT
)
INSERT INTO ManyToMany (Id, FKey, UseId) VALUES (1, 1, 2251)
INSERT INTO ManyToMany (Id, FKey, UseId) VALUES (2, 1, 2252)
INSERT INTO ManyToMany (Id, FKey, UseId) VALUES (3, 2, 2253)
INSERT INTO ManyToMany (Id, FKey, UseId) VALUES (4, 2, 2262)
INSERT INTO ManyToMany (Id, FKey, UseId) VALUES (5, 3, 2254)
INSERT INTO ManyToMany (Id, FKey, UseId) VALUES (6, 3, 2257)
INSERT INTO ManyToMany (Id, FKey, UseId) VALUES (7, 3, 2258)
INSERT INTO ManyToMany (Id, FKey, UseId) VALUES (8, 4, 2255)
INSERT INTO ManyToMany (Id, FKey, UseId) VALUES (9, 4, 2260)
INSERT INTO ManyToMany (Id, FKey, UseId) VALUES (10, 5, 2259)
INSERT INTO ManyToMany (Id, FKey, UseId) VALUES (11, 5, 2269)
SELECT * FROM Uniques
SELECT * FROM Duplicates
SELECT * FROM ManyToMany
UPDATE m
SET m.UseId = u.UseId
FROM ManyToMany m
JOIN Duplicates d ON m.UseId = d.UseId
JOIN Uniques u ON d.[Use] = u.[Use];
SELECT * FROM ManyToMany
DROP TABLE Uniques
DROP TABLE Duplicates
DROP TABLE ManyToMany
因爲數據是不完整的,這是令人困惑的。你從哪裏得到ID和FKey?你現在如何創建你的'多對多'表? – Jenn
人們不禁首先想到你陷入了這個混亂局面,但是Kevin Suchlicki的回答看起來是正確的。 –
在多對多表中,ID是INT IDENTITY字段。外鍵只是指向另一個不相關的表(可能應該不在本例中)。 – imdandman