0

For testing in Toad, I have the following code

select ...
from ...
where term_code = :termcode AND
        (
          case 
           when :theSubject is not null then SUBJ_CODE = :theSubject
           else 1 = 1
          end
        )          
AND ptrm_code <> 8

In short: If theSubject is not entered (is null) I want to display all the courses, otherwise I want to display only those where subject_code is the same as the one entered in the variable window in Toad.

But I get an error: [Error] Execution (77: 68): ORA-00905: missing keyword in here: when :theCourse is not null then sect.SSBSECT_SUBJ_CODE = theCourse

2
  • 1
    A case expression lets you have a value or expression as the then or else result; not a condition, which is what you are trying to do. Commented Jun 25, 2020 at 14:52
  • 1
    The statement you show mentions :theSubject but the error message mentions :theCourse; thus it seems that the error message does not refer to the statement shown. Also, in the error message the second occurrence of theCourse is not preceded by a colon. ??? Commented Jun 25, 2020 at 14:57

1 Answer 1

3

You can use boolean logic:

where 
    term_code = :termcode 
    and (:theSubject is null or subj_code = :theSubject)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.