【问题标题】:Case expression syntax error near "as"“as”附近的案例表达式语法错误
【发布时间】:2013-01-18 21:56:08
【问题描述】:

我的 case 表达式出现错误。我只想评估一个条件,并返回正确的语句。但我在“as”附近不断收到语法错误。

这是我所拥有的:

left outer join (            
        SELECT wbs1, wbs2, wbs3  
    , case 
            when (ProgramAffiliation = magrann.[programAffiliation_AEP/COHesh2010]()) 
            then '[AEP] as ''AEP1'''
            else [AEP Base] as 'AEP1'
        end
    , [AEP:Non-Compliant Mechanical Ventilation] as 'AEP2'  
    , [AEP - Non Energy Star AC ($90 deduction)] as 'AEP3'  
    , [AEP: Bonus] as 'AEP4'

【问题讨论】:

  • 缺少结束报价 then '[AEP]' as ''AEP1''(或错误的开始报价)
  • 语法高亮显示出了什么问题...
  • 另外,停止对别名使用AS 'foo' 语法。使用AS foo,或者,如果需要分隔,使用AS [foo]。使用单引号来分隔别名已被弃用并且它令人困惑(很多人认为它是一个字符串,这就是它的样子)。
  • 感谢反馈...关于别名和语法错误。看不到森林或树木。

标签: sql syntax case


【解决方案1】:

您的CASE 语法错误。您有一个用单引号括起来的列名,并且在两个地方都有别名(都是错误的):

 , case 
    when (ProgramAffiliation = magrann.[programAffiliation_AEP/COHesh2010]()) 
    then '[AEP] as ''AEP1'''  -- <-- don't put the column in single quotes and no alias here
    else [AEP Base] as 'AEP1'  -- < don't put the alias here
   end  -- < the alias goes here

所以你的CASE 应该是:

, case 
    when (ProgramAffiliation = magrann.[programAffiliation_AEP/COHesh2010]()) 
    then [AEP]
    else [AEP Base] 
    end as AEP1  

别名在CASE 表达式中的END 之后。

【讨论】:

    【解决方案2】:

    你应该在case 之前有end as

    case 
      when (ProgramAffiliation = magrann.[programAffiliation_AEP/COHesh2010]()) then [AEP] as 
      else [AEP Base] 
    end as [AEP1]
    // not else [AEP Base]  as 'AEP1' end
    

    更新

    【讨论】:

    • 您确实意识到您的答案不正确。您在看似列名[AEP] 周围有单引号,并且用户实际上在两个地方使用了别名。
    • @bluefeet : 是的,谢谢...我没有意识到在一行中可能会犯这么多错误:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 2013-09-06
    • 1970-01-01
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    相关资源
    最近更新 更多