你如何在同一个查询中两次更新?

时间:2015-09-08 15:45:04

标签: ms-access jet-sql

我试图在同一个查询中两次使用UPDATE来修改我的TABLE数据。你能解释一下我是怎么做到的吗?我在以下代码中收到sytax错误:

UPDATE HRBI SET HRBI.PayGroupLocationPayGroupCountryCode = IIF(HRBI.PayGroupLocationPayGroupCountryCode = 'DEU' AND HRBI.ManagementLevel = 'VP', 'DEU - Exec',
IIF(HRBI.PayGroupLocationPayGroupCountryCode = 'DEU' AND HRBI.ManagementLevel = 'SVP', 'DEU - Exec',
IIF(HRBI.PayGroupLocationPayGroupCountryCode = 'DEU' AND HRBI.ManagementLevel = 'FEL', 'DEU - Exec',
IIF(HRBI.PayGroupLocationPayGroupCountryCode = 'DEU' AND HRBI.ManagementLevel = 'DIR', 'DEU - Exec',
IIF(HRBI.PayGroupLocationPayGroupCountryCode = 'DEU' AND HRBI.ManagementLevel = 'SFL', 'DEU - Exec',
IIF(HRBI.PayGroupLocationPayGroupCountryCode = 'DEU' AND HRBI.ManagementLevel = 'STR', 'DEU - Exec',
IIF(HRBI.PayGroupLocationPayGroupCountryCode = 'DEU' AND HRBI.ManagementLevel = 'EVP', 'DEU - Exec', 
IIF(HRBI.WorkerID = '45' OR HRBI.WorkerID = '46' OR HRBI.WorkerID = '47' OR HRBI.WorkerID = '48' OR HRBI.WorkerID = '49' OR HRBI.WorkerID = '50' OR HRBI.WorkerID = '51', 'DEU - Exec', HRBI.PayGroupLocationPayGroupCountryCode))))))))
UPDATE SET HRBI.PayGroupCountryDesc = IIF(HRBI.PayGroupLocationPayGroupCountryCode = 'DEU - Exec', "Germany - Exec',HRBI.PayGroupCountryDesc);

我尝试在另一个UPDATE的末尾添加一个额外的UPDATE。另外,有没有办法在Access SQL中添加COMMENT?谢谢!

1 个答案:

答案 0 :(得分:0)

那是Mad SQL。

有效的set语句表示如果一行中的某些字段符合某些条件,则将值设为HRBI.PayGroupLocationPayGroupCountryCode =' DEU - Exec'否则保持原样。

"某些标准"应该作为where子句实现!

所以它变成了:

    UPDATE HRBI 
    SET HRBI.PayGroupLocationPayGroupCountryCode = 'DEU - Exec'
    WHERE 
    ( HRBI.PayGroupLocationPayGroupCountryCode = 'DEU' 
      AND 
      HRBI.ManagementLevel IN ('VP', 'SVP', ...etc ...)
    )
    OR
    (    HRBI.WorkerID = '45' OR HRBI.WorkerID = '46' OR HRBI.WorkerID = '47' 
      OR HRBI.WorkerID = '48' OR HRBI.WorkerID = '49' OR HRBI.WorkerID = '50' 
      OR HRBI.WorkerID = '51'
    );

请注意,你可以使用HRBI.WorkerID IN(45,46,.....)但我会留给你这样做......

您还需要重写:

    UPDATE SET HRBI.PayGroupCountryDesc =         
    IIF(HRBI.PayGroupLocationPayGroupCountryCode = 'DEU - Exec'
        , "Germany -   Exec'
        ,HRBI.PayGroupCountryDesc);

也许像这样嵌入第一个SQL

    UPDATE HRBI 
    SET HRBI.PayGroupLocationPayGroupCountryCode = 'DEU - Exec'

       ,HRBI.PayGroupCountryDesc =  "Germany -   Exec'

    WHERE 
    ( HRBI.PayGroupLocationPayGroupCountryCode = 'DEU' 
      AND 
      HRBI.ManagementLevel IN ('VP', 'SVP', ...etc ...)
    )
    OR
    (    HRBI.WorkerID = '45' OR HRBI.WorkerID = '46' OR HRBI.WorkerID = '47' 
      OR HRBI.WorkerID = '48' OR HRBI.WorkerID = '49' OR HRBI.WorkerID = '50' 
      OR HRBI.WorkerID = '51'
    );

OK?