我在让MSSQL与我的意图合作时遇到了一些麻烦。
检查以下内容......
重复
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替换“复制”多对多表中的UseIds。 ..
由此产生的良好数据看起来像这样......
好多对多......
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
感谢任何帮助
回应其中一个发布的答案......我遇到了一些困难。它似乎没有用。
更新命令正在更改“多对多”表中的记录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
答案 0 :(得分:3)
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];