简单的问题:
我有一个针对存储过程参数中提供的选择语句运行的游标,因此列数未知。
问题是如何打印列?
示例代码:
CREATE OR REPLACE Procedure MyExampleProcedure(P_QUERY VARCHAR2, P_dest VARCHAR2, P_file VARCHAR2)
AS
--Declaring types to use it for variables declarations
-----------------------------------------------
TYPE cursor_ref IS REF CURSOR;
TYPE typ_new_code IS TABLE OF VARCHAR2(50);
TYPE typ_new_desc IS TABLE OF VARCHAR2(500);
--Declaring variables based on previous types
-----------------------------------------------
c1 cursor_ref;
new_code typ_new_code;
new_desc typ_new_desc;
-----------------------------------------------
V_DEST_FILE utl_file.file_type;
BEGIN
V_DEST_FILE := utl_file.fopen(P_dest, P_file,'W');
OPEN c1 for P_QUERY;
FETCH C1 BULK COLLECT INTO new_code,new_desc;--This is the problematic area, not being able to iterate over the columns in the fetched row dynamically !
FOR I IN new_code.first .. new_code.last LOOP
utl_file.put(V_DEST_FILE, new_desc(i));
END LOOP;
utl_file.fclose(V_DEST_FILE);
EXCEPTION
WHEN NO_DATA_FOUND THEN
--Whatever
WHEN OTHERS THEN
--Whatever
END;
如您所见,这仅适用于具有两列的 select 语句,code
并且desc
与名称无关