使用视图中的记录更新空表

时间:2017-11-14 20:59:05

标签: set sql-update left-join inner-join

我正在尝试用视图中的记录([LA_Temp]。[dbo]。[vClaim] t2)更新我刚创建的空表([LA_Temp]。[dbo]。[LA_MCO_EQR_MED_201612_20171116] t1)。我知道更新语句因为连接而无法正常工作。由于t1是一个新的(空)表,因为连接,我不会返回任何结果。完成此任务的最佳方法是什么?我知道我错过了一些简单的事情。

Update [LA_Temp].[dbo].[LA_MCO_EQR_MED_201612_20171116] --empty table 

Set [Claim_Sys_ICN] = t2.ClaimID,
Claim_Line_Number = ClaimLine,
MCO_Claim_Status = Case When ClaimStatus = 'PAID' Then 'P'
                    When ClaimStatus = 'ADJUCATED' Then 'A'
                    When ClaimStatus = 'DENIED' Then 'D'
                    When ClaimStatus = 'REVERSED' Then 'R'
                    When ClaimStatus = 'VOID' Then 'V' End,
Patient_Account_Number = CarrierMemID,
MCO_Paid_Date = PaidDate,
Paid_Provider_NPI = PayToNPI,
Procedure_Code = ProcCode,
Procedure_Code_Modifier_1 = Modifier,
Procedure_Code_Modifier_2 = Modifier2,
Procedure_Code_Modifier_3 = Modifier3,
Procedure_Code_Modifier_4 = Modifier4,
Service_Provider_NPI =  RenderingNPI,
HDR_Clm_Paid_Amount = AmountPaid  

FROM [LA_Temp].[dbo].[LA_MCO_EQR_MED_201612_20171116] t1  --empty table

LEFT JOIN [LA_Temp].[dbo].[vClaim] t2 on --need records from this view
t1.[Claim_Sys_ICN] = t2.ClaimID  

Where PaidDate Between '12/1/2016' and '12/31/2016' 

同样的想法使用INSERT INTO ......

INSERT INTO [LA_Temp].[dbo].[LA_MCO_EQR_MED_201612_20171116] 
([Claim_Sys_ICN], Claim_Line_Number, 
MCO_Claim_Status, Patient_Account_Number, MCO_Paid_Date, Paid_Provider_NPI,
Procedure_Code, Procedure_Code_Modifier_1, Procedure_Code_Modifier_2, 
Procedure_Code_Modifier_3, Procedure_Code_Modifier_4,
Service_Provider_NPI, HDR_Clm_Paid_Amount)  

Select ClaimID, ClaimLine, Case When ClaimStatus = 'PAID' Then 'P'
                    When ClaimStatus = 'ADJUCATED' Then 'A'
                    When ClaimStatus = 'DENIED' Then 'D'
                    When ClaimStatus = 'REVERSED' Then 'R'
                    When ClaimStatus = 'VOID' Then 'V' End,
CarrierMemID, PaidDate, PayToNPI, ProcCode, Modifier, Modifier2, Modifier3, 
Modifier4, RenderingNPI, AmountPaid  

FROM [LA_Temp].[dbo].[vClaim]  

Where PaidDate Between '12/1/2016' and '12/31/2016'

1 个答案:

答案 0 :(得分:0)

执行INSERT而不是UPDATE。