将查询副本源更新到目标表访问2010

时间:2018-12-07 20:34:17

标签: sql ms-access

只是想弄清楚如何做:

我有2个表(tbl_WBint:目标表,temp_Agtpinwb:源表),需要使用更新查询来复制tbl_WBint中的字段。问题是,最初在应用更新查询之前,我使用查询使用tbl_WBint初始化0中的所有列。在使用下面的SQL更新查询后,它用temp_Agtpinwb表中的字段替换了某些行,这是我要执行的操作,但初始化查询期间具有零的其他行则为空白。如果源表中的表项需要更新到目标表,而目标表中的其他值保持为0,是否有方法仅运行更新查询?

希望如此。

UPDATE tbl_WBint 
INNER JOIN temp_Agtpinwb 
    ON tbl_WBint.[Agent Pin] = temp_Agtpinwb.[Agent Pin]
SET 
    tbl_WBint.[Agent Pin] = [temp_Agtpinwb]![Agent Pin], 
    tbl_WBint.[WB Current] = [temp_Agtpinwb]![WB Current], 
    tbl_WBint.[WB Prior Year] = [temp_Agtpinwb]![WB Prior Year],
    tbl_WBint.[WB Prior Year 3] = [temp_Agtpinwb]![WB Prior Year 3],
    tbl_WBint.[WB Jan Count] = [temp_Agtpinwb]![WB Jan Count], 
    tbl_WBint.[WB Feb Count] = [temp_Agtpinwb]![WB Feb Count],
    tbl_WBint.[WB Mar Count] = [temp_Agtpinwb]![WB Mar Count], 
    tbl_WBint.[WB Apr Count] = [temp_Agtpinwb]![WB Apr Count], 
    tbl_WBint.[WB May Count] = [temp_Agtpinwb]![WB May Count],
    tbl_WBint.[WB Jun Count] = [temp_Agtpinwb]![WB Jun Count], 
    tbl_WBint.[WB Jul Count] = [temp_Agtpinwb]![WB Jul Count], 
    tbl_WBint.[WB Aug Count] = [temp_Agtpinwb]![WB Aug Count], 
    tbl_WBint.[WB Sep Count] = [temp_Agtpinwb]![WB Sep Count],
    tbl_WBint.[WB Oct Count] = [temp_Agtpinwb]![WB Oct Count],
    tbl_WBint.[WB Nov Count] = [temp_Agtpinwb]![WB Nov Count], 
    tbl_WBint.[WB Dec Count] = [temp_Agtpinwb]![WB Dec Count];

谢谢

2 个答案:

答案 0 :(得分:0)

仅对IIF()使用条件逻辑来检查NULL,并保留原始值或更新源值。并考虑使用如下所示的表别名来减少长表名。

UPDATE tbl_WBint w
INNER JOIN temp_Agtpinwb a
    ON w.[Agent Pin] = a.[Agent Pin]
SET 
    w.[Agent Pin] = IIF(a.[Agent Pin] IS NULL, w.[Agent Pin], a.[Agent Pin]), 
    w.[WB Current] = IIF(a.[WB Current] IS NULL, w.[WB Current], a.[WB Current]), 
    w.[WB Prior Year] = IIF(a.[WB Prior Year] IS NULL, w.[WB Prior Year], a.[WB Prior Year]),
    ...

答案 1 :(得分:0)

听起来temp_Agtpinwb中的某些值是空值,并且正在覆盖您的0'd值,您可以使用

UPDATE tbl_WBint 
INNER JOIN temp_Agtpinwb 
    ON tbl_WBint.[Agent Pin] = temp_Agtpinwb.[Agent Pin]
SET 
    tbl_WBint.[Agent Pin] = [temp_Agtpinwb]![Agent Pin], 
    tbl_WBint.[WB Current] = Nz([temp_Agtpinwb]![WB Current],0), 
    tbl_WBint.[WB Prior Year] = Nz([temp_Agtpinwb]![WB Prior Year],0),
    tbl_WBint.[WB Prior Year 3] = Nz([temp_Agtpinwb]![WB Prior Year 3],0),
    tbl_WBint.[WB Jan Count] = Nz([temp_Agtpinwb]![WB Jan Count],0), 
    tbl_WBint.[WB Feb Count] = Nz([temp_Agtpinwb]![WB Feb Count],0),
    tbl_WBint.[WB Mar Count] = Nz([temp_Agtpinwb]![WB Mar Count],0), 
    tbl_WBint.[WB Apr Count] = Nz([temp_Agtpinwb]![WB Apr Count],0), 
    tbl_WBint.[WB May Count] = Nz([temp_Agtpinwb]![WB May Count],0),
    tbl_WBint.[WB Jun Count] = Nz([temp_Agtpinwb]![WB Jun Count],0), 
    tbl_WBint.[WB Jul Count] = Nz([temp_Agtpinwb]![WB Jul Count],0), 
    tbl_WBint.[WB Aug Count] = Nz([temp_Agtpinwb]![WB Aug Count],0), 
    tbl_WBint.[WB Sep Count] = Nz([temp_Agtpinwb]![WB Sep Count],0),
    tbl_WBint.[WB Oct Count] = Nz([temp_Agtpinwb]![WB Oct Count],0),
    tbl_WBint.[WB Nov Count] = Nz([temp_Agtpinwb]![WB Nov Count],0), 
    tbl_WBint.[WB Dec Count] = Nz([temp_Agtpinwb]![WB Dec Count],0);

NZ会将您的空值替换为零。 如果是这种情况,我相信您也不需要“初始化查询”。