Show your two ways to do same thing in PL/SQL function base on Oracle 9i.
Way 1: Loop
- /**
- * 根据工程获取相应的供应商,即设计、监理、施工单位
- *
- * Zhou Danyong 2011-01-13
- * 参数:P_TASKID 根据工程id从BO_PHASE_TASK_IS获取的TASKID
- * P_TYPE 类型
- */
- CREATE OR REPLACE FUNCTION F_GETSUPPLIERBYTYPE(P_TASKID IN VARCHAR2,
- P_TYPE IN VARCHAR2)
- RETURN VARCHAR2 IS
- V_SUPPLIER VARCHAR2(100) default null;
- V_S VARCHAR2(1000) default null;
- STR varchar2(20) default null;
- CURSOR C_CURSOR IS
- SELECT DISTINCT T.SUPPLIERNAME
- FROM BO_TASK_SUPPLIER T
- WHERE T.APPID = P_TASKID
- AND APP_SUPPLIER_TYPE LIKE '%' || P_TYPE || '%';
- BEGIN
- OPEN C_CURSOR;
- LOOP
- FETCH C_CURSOR
- INTO V_SUPPLIER;
- EXIT WHEN C_CURSOR%NOTFOUND;
- V_S := V_S || STR || V_SUPPLIER;
- STR := ',';
- END LOOP;
- CLOSE C_CURSOR;
- RETURN(V_S);
- END F_GETSUPPLIERBYTYPE;
Way 2: By DBMS_UTILITY
- /**
- * 根据工程获取相应的供应商,即设计、监理、施工单位
- *
- * Zhou Danyong 2011-01-13
- * 参数:P_TASKID 根据工程id从BO_PHASE_TASK_IS获取的TASKID
- * P_TYPE 类型
- */
- CREATE OR REPLACE FUNCTION F_GETSUPPLIERBYTYPE_WAY2(P_TASKID IN VARCHAR2,
- P_TYPE IN VARCHAR2)
- RETURN VARCHAR2 IS
- C_ARR DBMS_UTILITY.UNCL_ARRAY;
- RESULT VARCHAR2(1000);
- V_NUM NUMBER;
- V_S VARCHAR2(1000);
- BEGIN
- SELECT DISTINCT T.SUPPLIERNAME BULK COLLECT
- INTO C_ARR
- FROM BO_TASK_SUPPLIER T
- WHERE T.APPID = P_TASKID
- AND APP_SUPPLIER_TYPE LIKE '%' || P_TYPE || '%';
- DBMS_UTILITY.TABLE_TO_COMMA(C_ARR, V_NUM, V_S);
- RETURN(V_S);
- END F_GETSUPPLIERBYTYPE_WAY2;
附件:http://down.51cto.com/data/2357449
本文转自danni505 51CTO博客,原文链接:http://blog.51cto.com/danni505/477858,如需转载请自行联系原作者