簡體   English   中英

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與Toad中變量window中輸入的相同的課程。

但是我收到一個錯誤:[錯誤]執行(77:68):ORA-00905:此處缺少關鍵字:當:theCourse is not null then sect.SSBSECT_SUBJ_CODE = theCourse

任何解決此問題的意見將不勝感激。 謝謝

您可以使用 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