Oracle游标动态切换表

时间:2018-02-23 11:27:46

标签: sql oracle cursor

大家好想问一下,有没有办法在oracle游标中动态切换表

cursor c_testing IS  SELECT a,b,c FROM test_table 

TYPE tbl_testing IS TABLE OF c_testing%ROWTYPE INDEX BY PLS_INTEGER;
rec_testing tbl_testing;

目前c_testing只是从test_table中进行选择,但我可以根据传入的参数切换到test_table_dly或test_table_mly。执行立即似乎不适用于我想的游标:(

1 个答案:

答案 0 :(得分:1)

REFCURSOR?

SQL> create or replace function f_test (par_id in number)
  2    return sys_refcursor
  3  is
  4    cur_r sys_refcursor;
  5  begin
  6    if par_id = 1 then
  7       open cur_r for select empno, ename, job, sal from emp;
  8    elsif par_id = 2 then
  9       open cur_r for select dname, loc from dept;
 10    end if;
 11
 12    return cur_r;
 13  end;
 14  /

Function created.

SQL> select f_test(1) from dual;

F_TEST(1)
--------------------
CURSOR STATEMENT : 1

CURSOR STATEMENT : 1

     EMPNO ENAME      JOB              SAL
---------- ---------- --------- ----------
      7839 KING       PRESIDENT       5000
      7698 BLAKE      MANAGER         2850
      7782 CLARK      MANAGER         2450
      7566 JONES      MANAGER         2975
      7788 SCOTT      ANALYST         3000
      7902 FORD       ANALYST         3000
      7369 SMITH      CLERK            800
      7499 ALLEN      SALESMAN        1600
      7521 WARD       SALESMAN        1250
      7654 MARTIN     SALESMAN        1250
      7844 TURNER     SALESMAN        1500
      7876 ADAMS      CLERK           1100
      7900 JAMES      CLERK            950
      7934 MILLER     CLERK           1300

14 rows selected.


SQL> select f_test(2) from dual;

F_TEST(2)
--------------------
CURSOR STATEMENT : 1

CURSOR STATEMENT : 1

DNAME          LOC
-------------- -------------
ACCOUNTING     NEW YORK
RESEARCH       DALLAS
SALES          CHICAGO
OPERATIONS     BOSTON


SQL>