The AskTOM team is taking a break over the holiday season, so we're not taking questions or responding to comments. Please have a happy and safe festive season and as always, thanks for being a member of the AskTOM community.
Thanks for the question, H.
Asked: January 14, 2020 - 9:22 am UTC
Last updated: January 17, 2020 - 8:52 am UTC
Version: 10g
Viewed 1000+ times
var T varchar2(1) exec :T := 'E'; var E number; exec :E := 7788; var N varchar2(20) exec :N := 'MILLER'; select empno, ename from emp where -- how to use case statement to vary the condition based on :T -- if :T = 'E' then the condition should be where empno = :E -- and if :T = 'N' then the condition should be where ename like :N
var T varchar2(1) exec :T := 'E'; var E number; exec :E := 100; var N varchar2(20) exec :N := 'Neena'; select employee_id, first_name from hr.employees where case when :T = 'E' then to_char ( :E ) when :T = 'N' then :N end = case when :T = 'E' then to_char ( employee_id ) when :T = 'N' then first_name end; EMPLOYEE_ID FIRST_NAME 100 Steven exec :T := 'N'; select employee_id, first_name from hr.employees where case when :T = 'E' then to_char ( :E ) when :T = 'N' then :N end = case when :T = 'E' then to_char ( employee_id ) when :T = 'N' then first_name end; EMPLOYEE_ID FIRST_NAME 101 Neena
begin if :T = 'E' then select ... into ... from hr.employees where employee_id = :E; elsif :T = 'N' then select ... into ... from hr.employees where first_name = :N; end if; end; /
H, January 15, 2020 - 7:35 am UTC
-- if :T = 'E' then the condition should be where empno = :E -- and if :T = 'N' then the condition should be where ename like :N
A reader, January 15, 2020 - 1:08 pm UTC
Rajeshwaran, Jeyabal, January 16, 2020 - 10:14 am UTC
demo@PDB1> create table t as 2 select object_id as empno, 3 object_name as ename, 4 created as hiredate, 5 data_object_id as comm 6 from all_objects; Table created. demo@PDB1> create index t_empno_idx on t(empno); Index created. demo@PDB1> create index t_ename_idx on t(ename); Index created. demo@PDB1> variable e number demo@PDB1> variable n varchar2(30) demo@PDB1> variable t varchar2(1) demo@PDB1> exec :t :='E'; :e := 55; PL/SQL procedure successfully completed. demo@PDB1> set autotrace traceonly explain statistics demo@PDB1> select * 2 from t 3 where ( empno = :e and :t = 'E' ) 4 or ( ename =:n and :t = 'N') ; Execution Plan ---------------------------------------------------------- Plan hash value: 2111273234 --------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 50 | 4 (0)| 00:00:01 | |* 1 | TABLE ACCESS BY INDEX ROWID BATCHED| T | 1 | 50 | 4 (0)| 00:00:01 | | 2 | BITMAP CONVERSION TO ROWIDS | | | | | | | 3 | BITMAP OR | | | | | | | 4 | BITMAP CONVERSION FROM ROWIDS | | | | | | |* 5 | INDEX RANGE SCAN | T_ENAME_IDX | | | 3 (0)| 00:00:01 | | 6 | BITMAP CONVERSION FROM ROWIDS | | | | | | |* 7 | INDEX RANGE SCAN | T_EMPNO_IDX | | | 1 (0)| 00:00:01 | --------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("ENAME"=:N AND :T='N' OR :T='E' AND "EMPNO"=TO_NUMBER(:E)) 5 - access("ENAME"=:N) 7 - access("EMPNO"=TO_NUMBER(:E)) Statistics ---------------------------------------------------------- 1 recursive calls 0 db block gets 3 consistent gets 1 physical reads 0 redo size 761 bytes sent via SQL*Net to client 608 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed demo@PDB1> exec :t :='N'; :n := 'Tom'; PL/SQL procedure successfully completed. demo@PDB1> select * 2 from t 3 where ( empno = :e and :t = 'E' ) 4 or ( ename =:n and :t = 'N') ; no rows selected Execution Plan ---------------------------------------------------------- Plan hash value: 2111273234 --------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 50 | 4 (0)| 00:00:01 | |* 1 | TABLE ACCESS BY INDEX ROWID BATCHED| T | 1 | 50 | 4 (0)| 00:00:01 | | 2 | BITMAP CONVERSION TO ROWIDS | | | | | | | 3 | BITMAP OR | | | | | | | 4 | BITMAP CONVERSION FROM ROWIDS | | | | | | |* 5 | INDEX RANGE SCAN | T_ENAME_IDX | | | 3 (0)| 00:00:01 | | 6 | BITMAP CONVERSION FROM ROWIDS | | | | | | |* 7 | INDEX RANGE SCAN | T_EMPNO_IDX | | | 1 (0)| 00:00:01 | --------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("ENAME"=:N AND :T='N' OR :T='E' AND "EMPNO"=TO_NUMBER(:E)) 5 - access("ENAME"=:N) 7 - access("EMPNO"=TO_NUMBER(:E)) Statistics ---------------------------------------------------------- 0 recursive calls 0 db block gets 6 consistent gets 0 physical reads 0 redo size 544 bytes sent via SQL*Net to client 597 bytes received via SQL*Net from client 1 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 0 rows processed demo@PDB1>
Chuck Jolley, January 16, 2020 - 6:16 pm UTC
select * from t where empno = :e and :t = 'E' union all select * from t where ename =:n and :t = 'N';
The Oracle documentation contains a complete SQL reference.