简体   繁体   English

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

[英]Oracle PLSQL using a dynamic variable in a where clause

For testing in Toad, I have the following code为了在 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

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.简而言之:如果未输入theSubject(为null),我想显示所有课程,否则我只想显示subject_code与Toad中变量window中输入的相同的课程。

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但是我收到一个错误:[错误]执行(77:68):ORA-00905:此处缺少关键字:当:theCourse is not null then sect.SSBSECT_SUBJ_CODE = theCourse

Any comments to solve this issue will be greatly appreciated.任何解决此问题的意见将不胜感激。 Thanks谢谢

You can use boolean logic:您可以使用 boolean 逻辑:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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