大量游标,一个流水线功能

时间:2015-07-22 19:31:47

标签: sql oracle plsql

我有这样的代码:

CREATE OR REPLACE TYPE person_typ AS OBJECT (ssn NUMBER(9), 
name VARCHAR2(30), address VARCHAR2(100));

CREATE OR REPLACE TYPE t_tab AS TABLE OF person_typ;

CREATE OR REPLACE FUNCTION my_pipe(p_in VARCHAR2) RETURN t_tab PIPELINED IS
     rec person_typ;

     CURSOR cur1 IS
          SELECT ssn, name, address FROM /* ... complex query */

     CURSOR cur2 IS
          SELECT ssn, name, address FROM /* ... complex query */

     CURSOR cur3 IS
          SELECT ssn, name, address FROM /* ... complex query */

     CURSOR cur4 IS
          SELECT ssn, name, address FROM /* ... complex query */

     CURSOR cur5 IS
          SELECT ssn, name, address FROM /* ... complex query */
BEGIN
    FOR cur IN cur1 LOOP
        rec := r_inc(cur.ssn, cur.name, cur.address);
        PIPE ROW(rec);
    END LOOP; 

    FOR cur IN cur2 LOOP
        rec := r_inc(cur.ssn, cur.name, cur.address);
        PIPE ROW(rec);
    END LOOP; 

    FOR cur IN cur3 LOOP
        rec := r_inc(cur.ssn, cur.name, cur.address);
        PIPE ROW(rec);
    END LOOP; 

    FOR cur IN cur4 LOOP
        rec := r_inc(cur.ssn, cur.name, cur.address);
        PIPE ROW(rec);
    END LOOP; 

    FOR cur IN cur5 LOOP
        rec := r_inc(cur.ssn, cur.name, cur.address);
        PIPE ROW(rec);
    END LOOP; 
END;

我的问题是我的流水线功能是oogly - 我必须有五个游标(这与我无关),但我不想有五个for循环。我更喜欢只有一个循环和一个PIPE ROW(rec)语句...是否可以将结果连接到一个集合然后将它们连接起来?

1 个答案:

答案 0 :(得分:5)

嗯,如何使用union all并且只使用一个游标?

 CURSOR cur1 IS
      SELECT ssn, name, address FROM /* ... complex query */
      UNION ALL
      SELECT ssn, name, address FROM /* ... complex query */
      UNION ALL
      SELECT ssn, name, address FROM /* ... complex query */
      UNION ALL
      SELECT ssn, name, address FROM /* ... complex query */
      UNION ALL
      SELECT ssn, name, address FROM /* ... complex query */