plsql返回记录集的方法利用table

    技术2022-05-18  10

     

    今天在开发的时候,要找出两个表中的不同记录。我一般是用exists的,但看到也可以这样

    SELECT T5,T6,T7,T8 FROM TAB02 WHERE      T5||T6||T7 NOT IN      (SELECT T1||T2||T3 FROM TAB01)

    把几个字段合并成一个字段,虽然很SB,但可以解决问题。

    下面是重点,集合对象,以前我都是用游标来出来记录集,但有了集合对象后就可以直接加载记录集了,下面是一个使用集合对象record,table的例子

    DECLARE       TYPE TEST_EMP IS RECORD      (       C1 AA.PI%TYPE,       C2 AA.PO%TYPE      );            type t_type is table of TEST_EMP;    ---type t_type is table of aa%rowtype;      v_type t_type;

    BEGIN     SELECT PI,PO BULK COLLECT INTO v_type     FROM AA     WHERE AA.PI <= 3;     for v_index in 1 .. v_type.count() loop         dbms_output.put_line(v_type(v_index).C1 || ' ' || v_type(v_index).C2);            end loop;END;

    下面我要解决怎样用过程返回一个集合。

    PL/SQL表---table()函数用法:利用table()函数,我们可以将PL/SQL返回的结果集代替table。simple example:1、table()结合数组:create or replace type t_test as object(id integer,rq date,mc varchar2(60));create or replace type t_test_table as table of t_test;create or replace function f_test_array(n in number default null) return t_test_tableas v_test t_test_table := t_test_table();beginfor i in 1 .. nvl(n,100) loopv_test.extend();v_test(v_test.count) := t_test(i,sysdate,'mc'||i);end loop;return v_test;end f_test_array;select * from table(f_test_array(10));/*2、table()结合PIPELINED函数:*/create or replace function f_test_pipe(n in number default null) return t_test_table PIPELINED as v_test t_test_table := t_test_table();begin for i in 1 .. nvl(n,100) looppipe row(t_test(i,sysdate,'mc'||i)); end loop; return; end f_test_pipe; /select * from table(f_test_pipe(20));/*3、table()结合系统包:*/create table test (id varchar2(20));insert into test values('1');commit;explain plan for select * from test;select * from table(dbms_xplan.display);

     


    最新回复(0)