The Code below works fine for my procedure. But I need to make an addition.
First is the StockCode and Price Code exist in companyB.dbo.invPrice and EndDate < CurrentDate I need to delete this record from companyB,invPrice and update ImportedPromoPricing.Completed to D
Second if I add a record from ImportedPromoPricing then I need to update ImportedPromoPricing.Completed to Y
Initial procedure is below.
begin
insert into companyB.dbo.InvPrice ([invprice].[StockCode],[invprice].[PriceCode],[invprice].[SellingPrice])
select ImportedPromoPricing.[StockCode],ImportedPromoPricing.[PriceCode],ImportedPromoPricing.[SellingPrice]
from ImportedPromoPricing
left join companyB.dbo.InvPrice on ImportedPromoPricing.StockCode = companyB.dbo.InvPrice.StockCode and ImportedPromoPricing.PriceCode = companyB.dbo.InvPrice.PriceCode
where companyB.dbo.InvPrice.StockCode is null and ImportedPromoPricing.[StartDate]<= Convert(datetime, Convert(int, GetDate())) and
(ImportedPromoPricing.[EndDate] >= Convert(datetime, Convert(int, GetDate())) or ImportedPromoPricing.[EndDate] is null)
update companyB.dbo.InvPrice
set SellingPrice = ImportedPromoPricing.SellingPrice
from ImportedPromoPricing
left join companyB.dbo.InvPrice on ImportedPromoPricing.StockCode = companyB.dbo.InvPrice.StockCode and ImportedPromoPricing.PriceCode = companyB.dbo.InvPrice.PriceCode
where companyB.dbo.InvPrice.StockCode is not null
end