SQL中使用Case语句的语法错误

时间:2013-11-06 17:38:06

标签: sql case case-when

如果When条件正确,那么我希望它被标记为'Suspended for Audit ....'如果它们不正确,那么它是空白的或由t.fstrTaskSource +'<填写em> TYP '+ t.fstrType语句(此部分已经有效)

SELECT  t.flngKey AS flngTaskKey,
t.fstrAccountType,
t.fstrTaskSource,
CASE    t.fstrCategory 
    WHEN    '' THEN '' 
    ELSE    t.fstrTaskSource + '_CAT_' + t.fstrCategory 
    END AS fstrCategory,
CASE    t.fstrType 
    WHEN    '' THEN '' 
    WHEN    (wd.fstrWorkType    = 'SUSIN1' -- I am getting a syntax error here on the = sign --
        AND wd.fstrOwner        =  ' ' 
        AND wd.flngworkkey      =  wr.flngworkkey 
        AND wr.fstrAccountType  <> '007' 
        AND wr.fblnOpen         =  1 
        AND EXISTS  
            (SELECT 1 
            FROM    tblIndicator i
            WHERE   i.fstrIndicator   = 'EIWTCH' 
            AND i.flngVer         = 0 
            AND i.flngAccountKey  = wd.flngAccountKey)) -- I am also getting an error here on the ) sign --
    THEN 'Suspended for Audit Indicator - EIC Watch For'
    ELSE    t.fstrTaskSource + '_TYP_' + t.fstrType 
    END AS fstrType

2 个答案:

答案 0 :(得分:3)

您的第二个Case Expression混合了Simple CaseSearched Case

CASE    t.fstrType 
  WHEN    '' THEN '' 
  WHEN    (wd.fstrWorkType    = 'SUSIN1' 

将其更改为Searched Case表达式:

CASE WHEN t.fstrType = '' THEN ''
     WHEN (wd.fstrWorkType    = 'SUSIN1'  ...

Two formats of Case Expression are:

--Simple CASE expression: 
CASE input_expression 
     WHEN when_expression THEN result_expression [ ...n ] 
     [ ELSE else_result_expression ] 
END 

--Searched CASE expression:
CASE
     WHEN Boolean_expression THEN result_expression [ ...n ] 
     [ ELSE else_result_expression ] 
END

答案 1 :(得分:0)

您正试图在同一时间使用CASE的两种语法

第一个:

case expression
    when "val1" then ..
    when "val2" then ..
end

第二个:

case
    when column = "val1" then ..
    when column2 = "val2" then ..
end

因此,请在第二个CASE

中使用此功能
CASE
    WHEN t.fstrType = '' THEN ''
    WHEN (wd.fstrWorkType = 'SUSIN1' ...