在Access 2013中使用SQL更新查询

时间:2015-09-04 20:00:00

标签: sql ms-access

我正在尝试使用SQL更新Microsoft Access中的查询。当我在查询表达式"中运行"语法错误(缺少运算符)时,我收到以下错误我已将以下代码添加到我的基本SELECT / FROM查询脚本的底部。

UPDATE [HRBI Query]
SET [HRBI Query].[PaySegmentMultiplier] = (CASE WHEN [PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'Above top segment' THEN 0 ELSE CASE
WHEN [PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'Below segment 1' THEN 1.35 ELSE CASE
WHEN [PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'S1' THEN 1.25 ELSE CASE
WHEN [PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'S2' THEN 1.15 ELSE CASE
WHEN [PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'S3' THEN .90 ELSE CASE
WHEN [PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'S4' THEN .60 ELSE CASE
WHEN [PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'S5' THEN .40 ELSE CASE
ELSE PaySegmentMultiplier
END
END
END
END
END
END) 
FROM [HRBI Query];

我是SQL新手。任何人都可以解释为什么我收到此错误以及如何解决?感谢。

编辑:

我尝试使用SWITCH,但仍然在sytax上收到错误。有什么想法吗?

SELECT SWITCH([PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'Above top segment',[HRBI Query].PaySegmentMultiplier = 0,
[PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'Below segment 1', [HRBI Query].PaySegmentMultiplier =1.35,
[PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'S1', [HRBI Query].PaySegmentMultiplier =1.25,
[PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'S2', [HRBI Query].PaySegmentMultiplier =1.15,
[PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'S3', [HRBI Query].PaySegmentMultiplier =.90,
[PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'S4', [HRBI Query].PaySegmentMultiplier =.60,
[PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'S5', [HRBI Query].PaySegmentMultiplier =.40, True,'Error') 
FROM [HRBI Query];

编辑:我试过这个但仍然是语法错误?

SET [HRBI Query].[PaySegmentMultiplier] = Switch (
[PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'Above top segment', 0, 
[PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'Below segment 1', 1.35, 
[PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'S1', 1.25, 
[PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'S2', 1.15, 
[PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'S3', .90, 
[PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'S4', .60, 
[PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'S5', .40, TRUE, PaySegmentMultiplier);

2 个答案:

答案 0 :(得分:0)

CASE WHEN .. ELSE .. END适用于Sql Server。

在Access中,您可以使用Switch功能:

Switch ( expression1, value1, expression2, value2, ... expression_n, value_n )

所以在你的情况下

UPDATE [HRBI Query]
SET [HRBI Query].[PaySegmentMultiplier] = Switch (
[PayGroupCountryDesc] = 'France'  AND BasePayRangeSegment = 'Above top segment', 0, 
[PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'Below segment 1', 1.35,
[PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'S1', 1.25
[...etc...]
[and to simulate ELSE]
True, PaySegmentMultiplier
)

或SELECT查询:

SELECT Switch ( ... ) AS NewPaySegmentMultiplier FROM [HRBI Query]

答案 1 :(得分:0)

在MS Access中,CASE/WHEN的最佳翻译是嵌套的IIF()功能。同样在使用Access'方言的更新SQL语句中,您不会在结尾处使用FROM子句。

唯一的挑战是跟踪括号,这些括号应等于IIF的数量。我在下面缩进以帮助可视化:

UPDATE [HRBI Query]
SET [HRBI Query].[PaySegmentMultiplier] = 
IIF([PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'Above top segment', 0,
   IIF([PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'Below segment 1', 1.35,
      IIF([PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'S1', 1.25,
        IIF([PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'S2', 1.15,
           IIF([PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'S3', .90, 
              IIF([PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'S4', .60, 
                 IIF([PayGroupCountryDesc] = 'Country' AND BasePayRangeSegment = 'S5', .40, 
                     PaySegmentMultiplier
                 )
              )
           )
        )
     )
  )
);