Oracle PLSQL 在 where 子句中使用动态变量

Oracle PLSQL using a dynamic variable in a where clause

为了在 Toad 中进行测试,我有以下代码

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

简而言之:如果没有输入theSubject(为null)我想显示所有课程,否则我只想显示subject_code与变量[=中输入的相同的那些23=] 在蟾蜍中。

但是我得到一个错误: [错误] 执行 (77:68):ORA-00905:缺少关键字 在这里: 当 :theCourse 不为空时 sect.SSBSECT_SUBJ_CODE = theCourse

任何解决此问题的意见将不胜感激。 谢谢

您可以使用布尔逻辑:

where 
    term_code = :termcode 
    and (:theSubject is null or subj_code = :theSubject)