Guest User

Untitled

a guest
Aug 14th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. Selecting multiple columns conditionally in Oracle10g
  2. SELECT column1,
  3. column2,
  4. column3,
  5. column4,
  6. column5,
  7. column6,
  8. --If this condition is not met the following columns are not selected
  9. IF password = password
  10. (
  11. conditional_column1,
  12. conditional_column2,
  13. conditional_column3
  14. )
  15. FROM source_table
  16. WHERE username = username
  17.  
  18. SQL> create or replace procedure dyn_emp( p_include_empno in boolean,
  19. 2 p_rc out sys_refcursor )
  20. 3 as
  21. 4 l_sql varchar2(1000);
  22. 5 begin
  23. 6 l_sql := 'SELECT ename ';
  24. 7 if( p_include_empno )
  25. 8 then
  26. 9 l_sql := l_sql || ', empno ';
  27. 10 end if;
  28. 11 l_sql := l_sql || ' from emp';
  29. 12 open p_rc for l_sql;
  30. 13 end;
  31. 14 /
  32.  
  33. Procedure created.
  34.  
  35. SQL> variable rc refcursor;
  36. SQL> exec dyn_emp( true, :rc );
  37.  
  38. PL/SQL procedure successfully completed.
  39.  
  40. SQL> print rc
  41.  
  42. ENAME EMPNO
  43. ---------- ----------
  44. SMITH 7369
  45. ALLEN 7499
  46. WARD 7521
  47. JONES 7566
  48. MARTIN 7654
  49. BLAKE 7698
  50. CLARK 7782
  51. SCOTT 7788
  52. KING 7839
  53. TURNER 7844
  54. ADAMS 7876
  55. JAMES 7900
  56. FORD 7902
  57. MILLER 7934
  58.  
  59. 14 rows selected.
  60.  
  61. SQL> exec dyn_emp( false, :rc );
  62.  
  63. PL/SQL procedure successfully completed.
  64.  
  65. SQL> print rc
  66.  
  67. ENAME
  68. ----------
  69. SMITH
  70. ALLEN
  71. WARD
  72. JONES
  73. MARTIN
  74. BLAKE
  75. CLARK
  76. SCOTT
  77. KING
  78. TURNER
  79. ADAMS
  80. JAMES
  81. FORD
  82. MILLER
  83.  
  84. 14 rows selected.
Add Comment
Please, Sign In to add comment