简体   繁体   中英

Sybase proc throws “Incorrect syntax near '='”

I have the following piece of a SELECT query inside a stored procedure i'm developing:

AND (
    CASE S.SWITCH
        WHEN 'A' THEN P.TEST = T.OPTION_1
        WHEN 'C' THEN P.TEST = T.OPTION_1 + T.OPTION_2
        WHEN 'G' THEN P.TEST = T.OPTION_3
        WHEN 'N' THEN TRUE
        ELSE FALSE
    END
)

I'm getting an Incorrect syntax near '=' error. Why would it complain about the first equals sign? It's a Sybase server if anyone is interested.

You case comparison should be something like below, if you are testing P.TEST value based on S.SWITCH case.

AND (
    P.TEST = 
    CASE 
        WHEN S.SWITCH = 'A' THEN T.OPTION_1
        WHEN S.SWITCH =  'C' THEN T.OPTION_1 + T.OPTION_2
        WHEN S.SWITCH = 'G' THEN T.OPTION_3
        WHEN S.SWITCH = 'N' THEN TRUE
        ELSE FALSE
    END
)

If you are comparing based on P.TEST and S.SWITCH, you can do either of following

  1. Blorgbeard already provided this answer

     AND ( (S.SWITCH = 'A' AND P.TEST = T.OPTION_1) OR (S.SWITCH = 'C' AND T.OPTION_1 + T.OPTION_2) OR (S.SWITCH = 'G' AND P.TEST = T.OPTION_3) OR (S.SWITCH = 'N') ) 
  2. If you want to make case statement work for this, following could be a possible solution.

     AND ( CASE 1 = WHEN S.SWITCH = 'A' AND P.TEST = T.OPTION_1 THEN 1 WHEN S.SWITCH = 'C' AND P.TEST = T.OPTION_1 + T.OPTION_2 THEN 1 WHEN S.SWITCH = 'G' AND P.TEST = T.OPTION_3 THEN 1 WHEN S.SWITCH = 'N' THEN 1 ELSE 0 END ) 

Boolean expressions don't work like that in SQL. You can reformulate your switch like this:

AND (
    (S.SWITCH = 'A' AND P.TEST = T.OPTION_1) OR
    (S.SWITCH = 'C' AND T.OPTION_1 + T.OPTION_2) OR
    (S.SWITCH = 'G' AND P.TEST = T.OPTION_3) OR
    (S.SWITCH = 'N')
)

I know this is more of a comment than an answer, but hopefully this will lead to an answer, and I need to do formatted code for this so...

I tried this in Mysql and it worked. Could you try something like this in Sybase and see what it returns? The point being, extract the part that failed and test it out and see if you can figure out exactly what is wrong. It may be that Sybase is pointing to that equal sign but something else is really what is confusing it.

set @miller:='C';
set @mtime:=2;
select CASE @miller
    WHEN 'A' THEN @mtime = 1
    WHEN 'C' THEN @mtime = 2
    WHEN 'G' THEN @mtime = 3
    WHEN 'N' THEN TRUE
    ELSE FALSE
    END

This returns a 1 since miller = 'C' takes it to check if mtime = 2, which is true, and that means a 1 or true in Mysql.

Could you try and isolate this bit of code like this in Sybase?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM