收录日期:2020/11/26 22:29:42 时间:2010-09-07 20:11:58 标签:sql

I have two queries below. how do i put them in one stored proc. both the queries are truncating and inserting records in two different tables.

QUERY 1

truncate table [PlanFinder].[InvalidAwps] go

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

Query 2

truncate table [PlanFinder].[MissingNdcs] go

INSERT INTO [PlanFinder].[MissingNdcs]

(Ndc) 

SELECT DISTINCT Ndc 

FROM 

    PlanFinder.InvalidAwps   

WHERE AwpUnitCost IS NULL  

Thanks and Appreciate any help

take out the GO statements and dump all the code in 1 proc

create procedure prBla
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

truncate table [PlanFinder].[MissingNdcs] 
INSERT INTO [PlanFinder].[MissingNdcs] (Ndc) 

SELECT DISTINCT Ndc 
FROM  PlanFinder.InvalidAwps   
WHERE AwpUnitCost IS NULL  
GO

you might want to add some error handling...what do you want to do...rollback all if one of the statements fails or not?