ORA-00905: грешка при липсваща ключова дума

Продължавам да получавам тази досадна грешка, когато пробвам този код:

 select 
    gam.acid,
        gam.foracid,
        gam.acct_name,
        gam.sol_id,
        sol.sol_desc,
        gam.gl_sub_head_code,
        case gsh.gl_code
             when cast(gl_code as int) >= 01 and cast(gl_code as int) <=13
                      or cast(gl_code as int) >= 15 and cast(gl_code as int) <=24 then 'A'
                 when cast(gl_code as int) >= 28 and cast(gl_code as int) <= 31
                     or cast(gl_code as int) >= 33 and cast(gl_code as int) <= 39
                     or cast(gl_code as int) = 41
                     or cast(gl_code as int) >= 43 and cast(gl_code as int) <= 48 then 'L'
                 when cast(gl_code as int) = 51
                     or cast(gl_code as int) = 53
                     or cast(gl_code as int) >= 55 and cast(gl_code as int) <= 57 then 'C'
                 when cast(gl_code as int) >= 61 and cast(gl_code as int) <= 70
                     or cast(gl_code as int) >= 72 and cast(gl_code as int) <= 81 then 'I'
                 when cast(gl_code as int) = 84 or cast(gl_code as int) = 85
                     or cast(gl_code as int) >= 87 and cast(gl_code as int) <= 94 then 'E'
                 when cast(gl_code as int) >= 97 and cast(gl_code as int) <= 99 then 'S'
                 else null
          end as gl_classification
    from tbaadm.gam

Имам чувството, че някои неща не са на мястото си в кода или съм забравил да направя/добавя нещо. Всяка помощ ще бъде оценена.


person JamesP    schedule 15.04.2014    source източник
comment
Срещу кой ред е отчетена грешката (след като сте променили case gsh.gl_code само на case, както е споменато в отговорите)? Сигурни ли сте, че е от case, а не от клаузата joins/where, която не сте показали?   -  person Alex Poole    schedule 15.04.2014
comment
това е на този ред when cast(gl_code as int) >= 01 and cast(gl_code as int) <=13 жабата винаги сочи знака >= точно до 01   -  person JamesP    schedule 15.04.2014


Отговори (2)


Конструкцията CASE има два алтернативни синтаксиса:

CASE foo WHEN 1 THEN 'blah' END
CASE WHEN foo=1 THEN 'blah' END

Опитвате се да използвате и двете едновременно:

case gsh.gl_code
when cast(gl_code as int) >= 01

Трябва да бъде:

  select case
         --gsh.gl_code  remove this 
            when cast(gl_code as int) >= 01 and cast(gl_code as int) <=13
                or cast(gl_code as int) >= 15 and cast(gl_code as int) <=24 then 'A'
            when cast(gl_code as int) >= 28 and cast(gl_code as int) <= 31
                or cast(gl_code as int) >= 33 and cast(gl_code as int) <= 39
                or cast(gl_code as int) = 41
                or cast(gl_code as int) >= 43 and cast(gl_code as int) <= 48 then 'L'
            when cast(gl_code as int) = 51
                or cast(gl_code as int) = 53
                or cast(gl_code as int) >= 55 and cast(gl_code as int) <= 57 then 'C'
            when cast(gl_code as int) >= 61 and cast(gl_code as int) <= 70
                or cast(gl_code as int) >= 72 and cast(gl_code as int) <= 81 then 'I'
            when cast(gl_code as int) = 84 or cast(gl_code as int) = 85
                or cast(gl_code as int) >= 87 and cast(gl_code as int) <= 94 then 'E'
            when cast(gl_code as int) >= 97 and cast(gl_code as int) <= 99 then 'S'
            else null
        end as gl_classification
   from tbaadm.gam
person Gaurav Soni    schedule 15.04.2014
comment
не работи. ще помогне ли, ако кажа, че първоначално съм включил други елементи в израза select? сега ще актуализирам кода. - person JamesP; 15.04.2014
comment
Вероятно е полезно да се обясни защо е необходимо премахването на препратката към тази колона (объркано използване на двата типа регистър). @JamesP - с това премахнато, все още получавате същата грешка? - person Alex Poole; 15.04.2014
comment
@JamesP - Gaurav е прав, не можете да използвате CASE по този начин. Сериозно. - person Álvaro González; 15.04.2014
comment
не, получавам друга грешка: ORA-00923: FROM keyword not found where expected - person JamesP; 15.04.2014
comment
@JamesP - премахнахте ли само частта gsh.gl_code или реда с case gsh.gl_code, премахвайки и ключовата дума case? - person Alex Poole; 15.04.2014
comment
Бях отговорил общо взето по същия начин, така че си позволих да обединя информацията си във вашия въпрос, за да избегна такава очевидна измама. Надявам се, че нямате нищо против. - person Álvaro González; 15.04.2014
comment
Еха. Не мога да повярвам, че пропуснах една малка подробност. Това case първоначално е в средата на израз select и забравих да сложа запетая след частта end as. добавяйки, че липсващата запетая, заедно с предложението на Gaurav, решиха проблема ми. - person JamesP; 15.04.2014

Можете да опитате да използвате скоби, за да дадете приоритет на условията И/ИЛИ. Например:

when (cast(gl_code as int) >= 01 and cast(gl_code as int) <=13)
    or (cast(gl_code as int) >= 15 and cast(gl_code as int) <=24) then 'A'

може да помогне...

person Federico    schedule 15.04.2014