2017-05-26 50 views
1

創建一個臨時表,我需要插入20個字段。前10個來自表值函數,它使用源表中的一些字段作爲參數。第二個10將來自同一個函數,使用源表中相同的字段值,但爲兩個年份和日期相關參數傳遞不同的值。從表值函數插入/更新每個記錄的多個字段

要麼我沒有拿出正確的問題來找到答案,要麼我的知識水平阻止我看到答案會如何滿足我的需求。

表和函數沒有共同的字段用於例如在聯接中。該函數只需從表格記錄中獲取值,計算並返回10個金額。

如何在插入或後續更新期間獲取所需的金額?

IF OBJECT_ID('tempdb..##NumHealthDeps') IS NOT NULL 
DROP TABLE ##NumHealthDeps 

    Create table ##NumHealthDeps (DNum int, DUnit int, 
    Social varchar(9), RecID int, MedPlan varchar(12), MedPrem numeric(6,2), 
    RetCode varchar(3), DepTypes varchar(2), HDepCount int, CompAmt numeric(6,2), 
    F1Amt numeric(6,2), F2Amt numeric(6,2), P1Amt numeric(6,2), P2Amt numeric(6,2), 
    P3Amt numeric(6,2), 
    D1Amt numeric(6,2), D2Amt numeric(6,2), D3Amt numeric(6,2), 
    D4Amt numeric(6,2), NewCompAmt numeric(6,2), NewF1Amt numeric(6,2), 
    NewF2Amt numeric(6,2), NewP1Amt numeric(6,2), NewP2Amt numeric(6,2), 
    NewP3Amt numeric(6,2), NewD1Amt numeric(6,2), NewD2Amt numeric(6,2), 
    NewD3Amt numeric(6,2), NewD4Amt numeric(6,2)) 

    INSERT INTO ##NumHealthDeps 
(DNum, DUnit, Social, RecID, MedPlan, MedPrem, RetCode, DepTypes, HDepCount, 
CompAmt, F1Amt, F2Amt, P1Amt, P2Amt, P3Amt, D1Amt, D2Amt, D3Amt, D4Amt, 
NewCompAmt, NewF1Amt, NewF2Amt, NewP1Amt, NewP2Amt, NewP3Amt, NewD1Amt, 
NewD2Amt, NewD3Amt, NewD4Amt) 
SELECT nev2.DistrictNumber, DistrictClassification, Ssn, nev2.SubscribersCount, 
nev2.CurrentPlanId_1, nev2.CurPlan12PayRate_1, 
nev2.RetireesRateCode_1 + nev2.RetireesRateCode_2 + nev2.RetireesRateCode_3, 
' ', 
COUNT(DependentsLastName) AS 'NumDeps', 
0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 
0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 
FROM NVEligViewForAccess nev2 
LEFT OUTER JOIN NVDepViewForAccess 
ON Ssn = SubSsn 
AND SubscribersCount = SubCnt 
WHERE ActiveCoverageYears = 20162017 
AND nev2.CurEffDate_1 > 0 
AND nev2.CurTermDate_1 = 0 
AND SUBSTRING(nev2.CurrentPlanId_1,1,2) NOT IN ('KA','EA','HM') 
AND nev2.CurrentPlanId_1 NOT LIKE '0%' 
AND nev2.DistrictClassification != 6 
AND nev2.DistrictNumber < 7000 
AND (
    DepNum IS NULL 
    OR (
     DepEffHeaCoverageDate_1 > 0 
     AND DepTerHeaCoverageDate_1 = 0 
     ) 
    ) 
GROUP BY nev2.DistrictNumber, DistrictClassification, Ssn, nev2.SubscribersCount, 
nev2.CurrentPlanId_1, nev2.CurPlan12PayRate_1, 
nev2.RetireesRateCode_1 + nev2.RetireesRateCode_2 + nev2.RetireesRateCode_3 
ORDER BY nev2.DistrictNumber, DistrictClassification, Ssn 

以下代碼顯示了使用與前10個數量字段的年份和日期相關的值傳遞的函數調用和參數。所有命名參數都是NVEligViewForAccess表中的字段。

dbo.GetAllPPORatesForPlanFunc(20162017, DistrictNumber, 'H', 
    CurrentPlanId_1, '', DistrictClassification, 
    CASE 
     WHEN DistrictClassification < 5 
     OR DistrictClassification BETWEEN 10 AND 49 
     OR DistrictClassification BETWEEN 71 AND 74 THEN 'N' 
    ELSE 'Y' 
    END, 
    'N', 
    CASE 
     WHEN DistrictClassification < 5 
     OR DistrictClassification BETWEEN 10 AND 49 
     OR DistrictClassification BETWEEN 71 AND 74 THEN 'Y' 
     ELSE 'N' 
    END, 
    CASE 
     WHEN RetireesRateCode_1 = ' ' 
      AND RetireesRateCode_2 = ' ' 
      AND RetireesRateCode_3 = ' ' THEN 'ACT' 
     ELSE RetireesRateCode_1 + RetireesRateCode_2 + RetireesRateCode_3 
    END, 
    Ssn, 0, 0, CAST('10/1/2016' AS datetime), NULL) 

謝謝。

+0

您發佈了一段巨大的文字牆,但沒有多少細節。我甚至無法弄清楚問題在這裏。 –

回答

0

引起我的注意的主要觀點是,當您編寫「表和函數沒有共用字段用於例如連接」時。

這種情況可以通過使用「交叉應用」來解決。交叉應用類似於連接,但不需要密鑰。對於一面的每條記錄,它執行另一面並將結果與​​初始記錄相關聯。

例如:

Select * from mytable 
Cross apply dbo.myTableFunction(mytable.fieldparameter) 

一旦你能夠加入到表和功能,我想你只需要在這個指令建立一個插件。

+0

我發現了一些引用交叉應用的答案,但沒有將其作爲可能的解決方案。感謝您抽出時間,並指出交叉應用是解決方案。我試了一下,它完美地工作。 –

相關問題