Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Bex
Aged Yak Warrior
580 Posts |
Posted - 2008-09-24 : 11:04:50
|
HiI need to update a table to reflect correct mappings of Product References. However, if the value to which the product ref will be updated to already exists, I want to remove it:I have a table #Sale with the following data:ProductRef StoreID2 13 110 120 1I want to update the values '10' and '20' (old refs) to the new refs (10=1, 20=2).However, if the ref already exists (in this instance, there is already a record for productRef 2 in #Sale), I want to remove it to a reporting table. See belowCREATE TABLE #ProductMapping( ProductRef INT, OldProductRef INT, StoreID INT)INSERT INTO #ProductMappingSELECT 1,10,1UNIONSELECT 2,20,1UNION SELECT 3,30,1UNIONSELECT 1,10,2UNIONSELECT 2,20,2UNION SELECT 3,30,2CREATE TABLE #Sale( ProductRef INT, StoreID INTCONSTRAINT PK_Sale PRIMARY KEY (ProductRef,StoreID))INSERT INTO #SaleSELECT 10,1UNIONSELECT 20,1UNION SELECT 3,1UNION SELECT 2,1CREATE TABLE #Report( ProductRef INT, StoreID INT, Date DATETIME)SELECT * FROM #Sale-- This throws a valid error as there cannot be more than one entry for a ProductRefUPDATE #SaleSET ProductRef = m.ProductRefFROM #Sale s INNER JOIN #ProductMapping m ON s.ProductRef = m.OldProductRefAND m.StoreID = s.StoreID-- INSERT INTO #Report the ProductRefs that cannot be mapped (already exist)-- DELETE FROM #Sales the ProductRefs that cannot be mapped-- RERUN the UPDATE script aboveGODROP TABLE #Sale DROP TABLE #ProductMappingDROP TABLE #Report Hearty head pats |
|
|
Bex
Aged Yak Warrior
580 Posts |
Posted - 2008-09-24 : 11:34:03
|
Don't worry, I worked it out:CREATE TABLE #ProductMapping( ProductRef INT, OldProductRef INT, StoreID INT)GOINSERT INTO #ProductMappingSELECT 1,10,1UNIONSELECT 2,20,1UNION SELECT 3,30,1UNIONSELECT 1,10,2UNIONSELECT 2,20,2UNION SELECT 3,30,2GOCREATE TABLE #Sale( ProductRef INT, StoreID INTCONSTRAINT PK_Sale PRIMARY KEY (ProductRef,StoreID))GOINSERT INTO #SaleSELECT 10,1UNIONSELECT 20,1UNION SELECT 30,1UNION SELECT 2,1UNION SELECT 1,1GOCREATE TABLE #Report( OldProductRef INT, ProductRef INT, StoreID INT, Date DATETIME)GO--SELECT * FROM #Sale-- ERROR--UPDATE #Sale--SET ProductRef = m.ProductRef--FROM #Sale s INNER JOIN #ProductMapping m ON s.ProductRef = m.OldProductRef--AND m.StoreID = s.StoreID-- INSERT INTO #Report the ProductRefs that cannot be mapped (already exist)INSERT INTO #Report ( OldProductRef, ProductRef, StoreID, Date )SELECT s.ProductRef AS OldProductRef, p.ProductRef AS ProductRef, s.StoreID, CURRENT_TIMESTAMPFROM #Sale sINNER JOIN #ProductMapping p ON p.OldProductRef = s.ProductRef AND p.StoreID = s.StoreIDINNER JOIN (SELECT DISTINCT StoreID, ProductRef FROM #Sale) s2 ON s2.StoreID = s.StoreID AND s2.ProductRef = p.ProductRefSELECT * FROM #Report-- DELETE FROM #Sales the ProductRefs that cannot be mappedDELETE FROM sFROM #Sale sINNER JOIN #ProductMapping p ON p.OldProductRef = s.ProductRef AND p.StoreID = s.StoreIDINNER JOIN (SELECT DISTINCT StoreID, ProductRef FROM #Sale) s2 ON s2.StoreID = s.StoreID AND s2.ProductRef = p.ProductRef-- RERUN the UPDATE script aboveUPDATE #SaleSET ProductRef = m.ProductRefFROM #Sale s INNER JOIN #ProductMapping m ON s.ProductRef = m.OldProductRefAND m.StoreID = s.StoreIDGODROP TABLE #Sale DROP TABLE #ProductMappingDROP TABLE #Report Hearty head pats |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-24 : 11:41:09
|
May be thisINSERT INTO #ReportSELECT s1.ProductRef,s1.StoreIDFROM #Sale sINNER JOIN #ProductMapping pmON pm.OldProductRef=s.ProductRefAND pm.StoreID=s.StoreIDINNER JOIN #Sale s1ON s1.ProductRef=pm.ProductRefAND s1.StoreID=pm.StoreIDDELETE sFROM #Sale sLEFT JOIN #ProductMapping pmON pm.OldProductRef=s.ProductRefAND pm.StoreID=s.StoreIDWHERE pm.OldProductRef IS NULLUPDATE sSET s.ProductRef=pm.ProductRefFROM #Sale sINNER JOIN #ProductMapping pmON pm.OldProductRef=s.ProductRefAND pm.StoreID=s.StoreIDLEFT JOIN #Sale s1ON s1.ProductRef=pm.ProductRefAND s1.StoreID=pm.StoreIDWHERE s1.StoreID IS NULL |
 |
|
|
|
|
|
|
|