从 Table 值函数插入/更新每条记录的多个字段

Insert / Update Multiple Fields of Every Record From Table Valued Function

正在创建临时 table,我需要插入 20 个金额字段。前 10 个将来自 table 值函数,使用源 table 中的一些字段作为参数。第二个 10 将来自相同的函数,使用来自源 table 的相同字段值,但为两年和日期相关参数传递不同的值。

要么我没有想出正确的问题来找到答案,要么我的知识水平让我看不到答案如何满足我的需要。

table 和函数没有可使用的公共字段,例如,在联接中。该函数只是从 table 记录中获取值,计算并返回 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 table.

的字段
    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)

谢谢。

引起我注意的重点是你写 "The table and the function do not have a common field to use, for example, in a join"。

这个场景可以通过使用"cross apply"来解决。交叉应用类似于连接,但它不需要密钥。对于一侧的每条记录,它执行另一侧并将结果与​​初始记录相关联。

例如:

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

一旦你能够加入 table 和函数,我认为你只需要在这条指令上构建一个插入。