oracle中查詢結果集為空,則得到一個默認值


有同事問我上述問題,我把我的實現思路寫出來。
子查詢把查詢的結果和默認的結果全部顯示。
父查詢通過偽列rownum來篩選,如果查詢有結果,就有幾條就顯示幾條,而不去顯示子查詢中的默認值;如果查詢沒有結果,那就把默認值顯示出來

舉例:

select * from (
select table_name from user_tables where rownum < 3
union all
select 'default' from dual )
where rownum <= (select case (select count(1) from user_tables where rownum <3) when 0 then 1 else (select count(1) from user_tables where rownum <3) end from dual);

這個查詢,對user_tables的限制條件需要一樣。通過調整限制條件中的記錄數量,可以實現:如果查詢有結果集,則有幾個就顯示幾個,如果查詢沒有結果集,就顯示default


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM