I have a stored procedure below, and I don't know if it's correct.
I am inserting records in table PlanFinder.InvalidAwps
, and deleting the same records from the PlanFinder.NdcAwp
table.
Also, can anybody help me with adding try catch in the same stored procedure?
Alter procedure PlanFinder.InsertInvalidRecords
as
Truncate table [PlanFinder].[InvalidAwps]
INSERT INTO [PlanFinder].[InvalidAwps](Ndc, AwpUnitCost)
SELECT DISTINCT P.Ndc Ndc, A.Price AwpUnitCost
FROM PlanFinder.PlanFinder.HpmsFormulary P
LEFT JOIN (SELECT Ndc, Price
FROM MHSQL01D.Drug.FdbPricing.vNdcPrices
WHERE PriceTypeCode = '01'
AND CurrentFlag = 1) A ON P.Ndc = A.Ndc
WHERE ( A.Ndc IS NULL
OR A.Price <= 0
OR A.Price IS NULL)
AND p.Ndc IS NOT NULL
DELETE FROM PlanFinder.NdcAwp
WHERE Ndc IN (SELECT Ndc
FROM PlanFinder.InvalidAwps)