To fix this, run the following stored procedure below:
/****** Object: StoredProcedure [dbo].[CheckUploadedDeliveries] Script Date: 08/15/2014 17:16:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Archelle pagapulan
-- Create date: July 4, 2014
-- Description: Delete/Corrects multiple uploaded deliveries
-- =============================================
CREATE PROCEDURE [dbo].[CheckUploadedDeliveries]
AS
BEGIN
SELECT
store.StoreCode, CASE(tr.POType) WHEN 1 THEN cast(tr.WorksheetID as varchar) ELSE 'N/A' END as ReferenceWorksheet,
case(tr.POType) WHEN 1 then 'MO - PurchaseOrder IN' WHEN 2 THEN 'Store - InventoryTransfer IN' WHEN 3 THEN 'Store - Inventory Transfer OUT'
WHEN 4 THEN 'Store - PurchaseOrder IN' END as POType,
tr.PONumber
,case(refStore.StoreCode) when null then 'N/A' ELSE isnull(refStore.StoreCode, 'N/A') end as ReferenceStore, tr.OtherPOID as ReferenceID,
tr.myCount as TotalCount,
'delete from PurchaseOrderEntry where PurchaseOrderID IN(select ID from PurchaseOrder where StoreID = ' + cast(tr.storeid as varchar) + '
and ponumber = ''' + tr.ponumber + '''
and potype = ' + cast(tr.potype as varchar) + ' and WorksheetID = ' + cast(tr.WorksheetID as varchar) + ' and OtherPOID = ' + cast(tr.OtherPOID as varchar) +
' and OtherStoreID = ' + CAST(tr.OtherStoreID AS varchar) + ' and ID
!= (select max(ID) FROM PurchaseOrder where StoreID = ' + cast(tr.storeid as varchar) + ' and ponumber = ''' + tr.ponumber + '''
and potype = ' + cast(tr.potype as varchar) + ' and WorksheetID = ' + cast(tr.WorksheetID as varchar) + ' and OtherPOID = ' + cast(tr.OtherPOID as varchar) +
' and OtherStoreID = ' + CAST(tr.OtherStoreID AS varchar) + ')) AND StoreID = ' + cast(tr.storeid as varchar)
AS SamePOEntryFirst,
'delete from PurchaseOrder where StoreID = ' + cast(tr.storeid as varchar) + ' and ponumber = ''' + tr.ponumber + '''
and potype = ' + cast(tr.potype as varchar) + ' and WorksheetID = ' + cast(tr.WorksheetID as varchar) + ' and OtherPOID = ' + cast(tr.OtherPOID as varchar) +
' and OtherStoreID = ' + CAST(tr.OtherStoreID AS varchar) + ' and ID != (select max(ID) FROM PurchaseOrder
where StoreID = ' + cast(tr.storeid as varchar) + ' and ponumber = ''' + tr.ponumber + '''
and potype = ' + cast(tr.potype as varchar) + ' and WorksheetID = ' + cast(tr.WorksheetID as varchar) + ' and OtherPOID = ' + cast(tr.OtherPOID as varchar) +
' and OtherStoreID = ' + CAST(tr.OtherStoreID AS varchar) + ')' AS deleteSamePONext,
'select * from PurchaseOrder where StoreID = ' + cast(tr.storeid as varchar) + ' and ponumber = ''' + tr.ponumber + '''
and potype = ' + cast(tr.potype as varchar) + ' and WorksheetID = ' + cast(tr.WorksheetID as varchar) + ' and OtherPOID = ' + cast(tr.OtherPOID as varchar) +
' and OtherStoreID = ' + CAST(tr.OtherStoreID AS varchar) as 'ViewDeliveries'
from (
select storeid, ponumber, potype, WorksheetID, OtherPOID, OtherStoreID, count(*) as myCount
from PurchaseOrder
group by storeid, ponumber, potype, WorksheetID,OtherPOID, OtherStoreID
having count(*) > 1 ) as tr left join store on store.ID = tr.StoreID
left join Store refStore on refStore.ID = tr.OtherStoreID
END
GO
Execute the procedure. Ex
DECLARE @return_value int
EXEC @return_value = [dbo].[CheckUploadedDeliveries]
SELECT 'Return Value' = @return_value
GO