Here's a summary of my method to reconcile Items created in StoreOperations instead of Headquarters. Everything seems to work great. The one casualty is the inventory count. Because there are so many duplicates and new items coming from HQ, I cannot know which is the correct count. I hope this helps someone else in the future. I'm soon to be performing a similar process on Suppliers and Customers. The SQL script details are listed below:
- Run Copy Table and Remove Duplicate Items.sql
- Perform WS101 Sync
- Perform WS250
- Perform WS401
--------------------------
- Run Correct Orphaned ItemID records.sql
- Perform WS101 Sync
- Perform WS250
- Perform WS401
--------------------------
- Perform WS501
- Perform Task 190
- Commit Store counts to HQ
Repeat Process for each store.
/
*******************************************************************************/
/* Copy Table and Remove Duplicate Items.sql /
*******************************************************************************/
--copy Items table for later use SELECT * INTO ItemCopy FROM Item
-- add a new column ALTER TABLE Item ADD NewPK int NULL go
-- populate the new Primary Key DECLARE @intCounter int SET @intCounter = 0 UPDATE Item SET @intCounter = NewPK = @intCounter + 1
-- ID the records to delete and get one primary key value also
-- We'll delete all but this primary key SELECT ItemLookupCode, RecCount=count(*), PktoKeep = max(NewPK) INTO #dupes FROM Item GROUP BY ItemLookupCode HAVING count(*) > 1 ORDER BY count(*) DESC, ItemLookupCode
-- delete dupes except one Primary key for each dup record DELETE Item FROM Item a join #dupes d ON d.ItemLookupCode = a.ItemLookupCode WHERE a.NewPK not in (SELECT PKtoKeep FROM #dupes)
-- remove the NewPK column ALTER TABLE Item DROP COLUMN NewPK go
DROP TABLE #dupes
/
*******************************************************************************/
/* Correct Orphaned ItemID records.sql /
*******************************************************************************/
--copy the orphaned Alias ids to a temp table SELECT DISTINCT Alias.ItemID as oldID, Item.ItemLookupCode INTO #Orphaned FROM Alias LEFT JOIN Item ON Alias.ItemID = Item.ID WHERE ItemLookupCode is null GO
ALTER TABLE #Orphaned ADD newID int NULL GO
--using the previously saved ItemCopy find the ILC UPDATE #Orphaned SET ItemLookupCode=b.ItemLookupCode FROM #Orphaned a, ItemCopy b WHERE a.oldID = b.ID
--now match the ILC to the new ID UPDATE #Orphaned SET newID=b.ID FROM #Orphaned a, Item b WHERE a.ItemLookupCode = b.ItemLookupCode
--now update the Alias table with corrected itemIDs UPDATE Alias SET ItemID=b.newID FROM Alias a, #Orphaned b WHERE (a.ItemID = b.oldID) AND (b.newID IS NOT NULL )
DROP TABLE #Orphaned
--copy the orphaned InventoryTransferLogids to a temp table SELECT DISTINCT InventoryTransferLog.ItemID as oldID, Item.ItemLookupCode INTO #Orphaned FROM InventoryTransferLog LEFT JOIN Item ON InventoryTransferLog.ItemID = Item.ID WHERE ItemLookupCode is null GO
ALTER TABLE #Orphaned ADD newID int NULL GO
--using the previously saved ItemCopy find the ILC UPDATE #Orphaned SET ItemLookupCode=b.ItemLookupCode FROM #Orphaned a, ItemCopy b WHERE a.oldID = b.ID
--now match the ILC to the new ID UPDATE #Orphaned SET newID=b.ID FROM #Orphaned a, Item b WHERE a.ItemLookupCode = b.ItemLookupCode
--now update the InventoryTransferLog table with corrected itemIDs UPDATE InventoryTransferLog SET ItemID=b.newID FROM InventoryTransferLog a, #Orphaned b WHERE (a.ItemID = b.oldID) AND (b.newID IS NOT NULL )
DROP TABLE #Orphaned
--copy the orphaned OrderEntryids to a temp table SELECT DISTINCT OrderEntry.ItemID as oldID, Item.ItemLookupCode INTO #Orphaned FROM OrderEntry LEFT JOIN Item ON OrderEntry.ItemID = Item.ID WHERE ItemLookupCode is null GO
ALTER TABLE #Orphaned ADD newID int NULL GO
--using the previously saved ItemCopy find the ILC UPDATE #Orphaned SET ItemLookupCode=b.ItemLookupCode FROM #Orphaned a, ItemCopy b WHERE a.oldID = b.ID
--now match the ILC to the new ID UPDATE #Orphaned SET newID=b.ID FROM #Orphaned a, Item b WHERE a.ItemLookupCode = b.ItemLookupCode
--now update the OrderEntry table with corrected itemIDs UPDATE OrderEntry SET ItemID=b.newID FROM OrderEntry a, #Orphaned b WHERE (a.ItemID = b.oldID) AND (b.newID IS NOT NULL )
DROP TABLE #Orphaned
--copy the orphaned SupplierList ids to a temp table SELECT DISTINCT SupplierList.ItemID as oldID, Item.ItemLookupCode INTO #Orphaned FROM SupplierList LEFT JOIN Item ON SupplierList.ItemID = Item.ID WHERE ItemLookupCode is null GO
ALTER TABLE #Orphaned ADD newID int NULL GO
--using the previously saved ItemCopy find the ILC UPDATE #Orphaned SET ItemLookupCode=b.ItemLookupCode FROM #Orphaned a, ItemCopy b WHERE a.oldID = b.ID
--now match the ILC to the new ID UPDATE #Orphaned SET newID=b.ID FROM #Orphaned a, Item b WHERE a.ItemLookupCode = b.ItemLookupCode
--now update the SupplierList table with corrected itemIDs UPDATE SupplierList SET ItemID=b.newID FROM SupplierList a, #Orphaned b WHERE (a.ItemID = b.oldID) AND (b.newID IS NOT NULL )
DROP TABLE #Orphaned
--copy the orphaned TransactionEntry ids to a temp table SELECT DISTINCT TransactionEntry.ItemID as oldID, Item.ItemLookupCode INTO #Orphaned FROM TransactionEntry LEFT JOIN Item ON TransactionEntry.ItemID = Item.ID WHERE ItemLookupCode is null GO
ALTER TABLE #Orphaned ADD newID int NULL GO
--using the previously saved ItemCopy find the ILC UPDATE #Orphaned SET ItemLookupCode=b.ItemLookupCode FROM #Orphaned a, ItemCopy b WHERE a.oldID = b.ID
--now match the ILC to the new ID UPDATE #Orphaned SET newID=b.ID FROM #Orphaned a, Item b WHERE a.ItemLookupCode = b.ItemLookupCode
--now update the TransactionEntry table with corrected itemIDs UPDATE TransactionEntry SET ItemID=b.newID FROM TransactionEntry a, #Orphaned b WHERE (a.ItemID = b.oldID) AND (b.newID IS NOT NULL )
DROP TABLE #Orphaned
--copy the orphaned TransactionHoldEntry ids to a temp table SELECT DISTINCT TransactionHoldEntry.ItemID as oldID, Item.ItemLookupCode INTO #Orphaned FROM TransactionHoldEntry LEFT JOIN Item ON TransactionHoldEntry.ItemID = Item.ID WHERE ItemLookupCode is null GO
ALTER TABLE #Orphaned ADD newID int NULL GO
--using the previously saved ItemCopy find the ILC UPDATE #Orphaned SET ItemLookupCode=b.ItemLookupCode FROM #Orphaned a, ItemCopy b WHERE a.oldID = b.ID
--now match the ILC to the new ID UPDATE #Orphaned SET newID=b.ID FROM #Orphaned a, Item b WHERE a.ItemLookupCode = b.ItemLookupCode
--now update the TransactionHoldEntry table with corrected itemIDs UPDATE TransactionHoldEntry SET ItemID=b.newID FROM TransactionHoldEntry a, #Orphaned b WHERE (a.ItemID = b.oldID) AND (b.newID IS NOT NULL )
DROP TABLE #Orphaned
--copy the orphaned PurchaseOrderEntry ids to a temp table SELECT DISTINCT PurchaseOrderEntry.ItemID as oldID, Item.ItemLookupCode INTO #Orphaned FROM PurchaseOrderEntry LEFT JOIN Item ON PurchaseOrderEntry.ItemID = Item.ID WHERE ItemLookupCode is null GO
ALTER TABLE #Orphaned ADD newID int NULL GO
--using the previously saved ItemCopy find the ILC UPDATE #Orphaned SET ItemLookupCode=b.ItemLookupCode FROM #Orphaned a, ItemCopy b WHERE a.oldID = b.ID
--now match the ILC to the new ID UPDATE #Orphaned SET newID=b.ID FROM #Orphaned a, Item b WHERE a.ItemLookupCode = b.ItemLookupCode
--now update the PurchaseOrderEntry table with corrected itemIDs UPDATE PurchaseOrderEntry SET ItemID=b.newID FROM PurchaseOrderEntry a, #Orphaned b WHERE (a.ItemID = b.oldID) AND (b.newID IS NOT NULL )
DROP TABLE #Orphaned