I'm having a bit of trouble getting MSSQL to cooperate with my intentions.
Examine the following...
Duplicates
UseId | Use ------------- 2254 | Primary 2255 | Primary 2256 | Primary 2257 | Intersection 2258 | Monitor 2259 | Primary 2260 | Intersection 2261 | Primary 2264 | Monitor 2269 | Secondary
Uniques
UseID | Use -------------- 2252 | Intersection 2253 | Monitor 2251 | Primary 2262 | Secondary
Many to Many Table
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
I want to run an update query on the "Many to Many" table that will match the use column on the duplicates and uniques, and then replace and "Duplicate" UseIds in the many to many table with their matching unique UseId...
The resulting good data would look like this....
Good Many to Many Table...
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
Any help is appreciated
In response to one of the posted answers... I'm having some difficulties. It doesn't seem to be working.
The update command is changing records 1-4 in the Many to Many table (even though they are good)... and not changing any of the other records.
This script provides an example of the problem.
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
UniquestoDuplicatesandDuplicatestoUniquesand his answer will yield the correct results.