2017-05-26 49 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

我发现了一些引用交叉应用的答案,但没有将其作为可能的解决方案。感谢您抽出时间,并指出交叉应用是解决方案。我试了一下,它完美地工作。 –

相关问题