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

时间:2021-10-21 23:11:21

If the When conditions are correct, then I want it to be labeled as 'Suspended for Audit....' if they are not correct, then have it be either blank or filled in by the t.fstrTaskSource + 'TYP' + t.fstrType statement (this part works already)

如果When条件是正确的,那么我希望它被标记为'Suspended for Audit ....'如果它们不正确,那么它是空白的还是由t.fstrTaskSource +'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 个解决方案

#1


3  

Your second Case Expression is a mix of Simple Case and Searched Case.

您的第二个案例表达是简单案例和搜索案例的混合。

I.e.

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

Change it to a Searched Case expression as:

将其更改为搜索的案例表达式:

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

#2


0  

Your are trying to use the two syntax of CASE in the same time

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

the first:

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

the second:

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

So, use this in your second CASE:

所以,在你的第二个CASE中使用它:

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

#1


3  

Your second Case Expression is a mix of Simple Case and Searched Case.

您的第二个案例表达是简单案例和搜索案例的混合。

I.e.

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

Change it to a Searched Case expression as:

将其更改为搜索的案例表达式:

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

#2


0  

Your are trying to use the two syntax of CASE in the same time

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

the first:

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

the second:

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

So, use this in your second CASE:

所以,在你的第二个CASE中使用它:

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