Question

The following stored procedure is not performing good. i wanted to change it into a Merge Statement. How do i do it? I have not used the Merge Statement before. Please help

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pa_CPE_TUA_RD]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pa_CPE_TUA_RD]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pa_CPE_TUA_RD]') AND type in (N'P', N'PC'))
BEGIN
CREATE PROCEDURE [dbo].[pa_CPE_TUA_RD]  
AS       
  -- This procedure moves RewardDistribution records from UploadTemp_RD    
  SET NOCOUNT ON;    
  declare @NumRecs int;    
  select @NumRecs=count(*) from CPE_UploadTemp_RD with (NoLock) where WaitingACK=-5;    
  if @NumRecs=0 begin    
    set rowcount 1800;     
    update CPE_UploadTemp_RD with (RowLock) set WaitingACK=-5;     
    set rowcount 0;    
  end    

  -- Move records to CPE_RewardDistribution    
  insert into CPE_RewardDistribution with (RowLock) (LocalID, ServerSerial, LocationID, IncentiveID, RewardOptionID, Phase, CustomerPK, DistributionDate, LogixTransNum, PresentedCustomerID, PresentedCardTypeID, ResolvedCustomerID, HHID, Deleted, LastUpdate, WaitingACK, LastServerID, POSTimeStamp)     
    select Col1, Col2, Col9, Col3, Col4, Col5, Col6, Col7, Col8, Col10, Col11, Col12, Col13, 0 as Deleted, getdate() as LastUpdate, -5 as WaitingACK, ServerSerial as LastServerID, POSTimeStamp from CPE_UploadTemp_RD with (NoLock)     
    where WaitingACK=-5;    

  -- Move records to CPE_RD_Output    
  insert into CPE_RD_Output with (RowLock) (LocalID, ServerSerial, IncentiveID, RewardOptionID, Phase, CustomerPK, DistributionDate, Deleted, LastUpdate, WaitingACK, TargetLocationID, POSTimeStamp)     
    select BT.Col1 as LocalID, BT.Col2 as ServerSerial, BT.Col3 as IncentiveID, BT.Col4 as RewardOptionID, BT.Col5 as Phase, BT.Col6 as CustomerPK, BT.Col7 as DistributionDate,     
    0 as Deleted, getdate() as LastUpdate, 0 as WaitingACK, CL.LocationID as TargetLocationID, BT.POSTimeStamp as POSTimeStamp     
    from CPE_UploadTemp_RD as BT with (NoLock) Inner Join CustomerLocations as CL with (NoLock) on BT.Col6=CL.CustomerPK and not(CL.LocationID=BT.LocationID) and BT.WaitingACK=-5;    

  Delete from CPE_UploadTemp_RD with (RowLock) where WaitingACK=-5;    

END
GO
GRANT EXECUTE ON [dbo].[pa_CPE_TUA_RD] TO [Copient_Logix_uspRole] AS [dbo]
GO

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top