Oracle - 从引用游标中选择特定列

时间:2012-04-25 18:25:56

标签: oracle stored-procedures cursor

我有一个名为Table1的表。它有很多列,其中一列是Column1。我不知道其他专栏,有时甚至可能会改变。有一个强类型的引用游标类型,它返回Table1%rowtype,名为cur_Table1。我有一个名为SP1的存储过程,其out参数类型为cur_Table1。我从另一个只看到这个存储过程的数据库调用这个SP1存储过程,而不是表或类型本身。如何从返回的游标中仅选择Column1?我知道我可以获取一个记录或变量,因为游标有列,但我只知道一个列的存在,所以我不能声明完整的记录或正确的变量数。

3 个答案:

答案 0 :(得分:7)

您可以使用DBMS_SQL执行此操作,但它并不漂亮。

表格和样本数据(COLUMN1的数字为1 - 10):

create table table1(column1 number, column2 date, column3 varchar2(1000), column4 clob);

insert into table1
select level, sysdate, level, level from dual connect by level <= 10;
commit;

打包一个程序,打开引用光标并选择所有内容:

create or replace package test_pkg is
    type cur_Table1 is ref cursor return table1%rowtype;
    procedure sp1(p_cursor in out cur_table1);
end;
/

create or replace package body test_pkg is
    procedure sp1(p_cursor in out cur_table1) is
    begin
        open p_cursor for select column1, column2, column3, column4 from table1;
    end;
end;
/

从ref光标读取COLUMN1数据的PL / SQL块:

--Basic steps are: call procedure, convert cursor, describe and find columns,
--then fetch rows and retrieve column values.
--
--Each possible data type for COLUMN1 needs to be added here.
--Currently only NUMBER is supported.
declare
    v_cursor sys_refcursor;
    v_cursor_number number;

    v_columns number;
    v_desc_tab dbms_sql.desc_tab;
    v_position number;
    v_typecode number;
    v_number_value number;
begin
    --Call procedure to open cursor
    test_pkg.sp1(v_cursor);
    --Convert cursor to DBMS_SQL cursor
    v_cursor_number := dbms_sql.to_cursor_number(rc => v_cursor);
    --Get information on the columns
    dbms_sql.describe_columns(v_cursor_number, v_columns, v_desc_tab);

    --Loop through all the columns, find COLUMN1 position and type
    for i in 1 .. v_desc_tab.count loop
        if v_desc_tab(i).col_name = 'COLUMN1' then
            v_position := i;
            v_typecode := v_desc_tab(i).col_type;

            --Pick COLUMN1 to be selected.
            if v_typecode = dbms_types.typecode_number then
                dbms_sql.define_column(v_cursor_number, i, v_number_value);
            --...repeat for every possible type.
            end if;
        end if;
    end loop;

    --Fetch all the rows, then get the relevant column value and print it
    while dbms_sql.fetch_rows(v_cursor_number) > 0 loop
        if v_typecode = dbms_types.typecode_number then
            dbms_sql.column_value(v_cursor_number, v_position, v_number_value);
            dbms_output.put_line('Value: '||v_number_value);
        --...repeat for every possible type
        end if;
    end loop;   
end;
/

答案 1 :(得分:3)

鉴于最初的问题,jonearles的答案仍然是正确的,所以我会将其标记为这样,但我最终做了一些完全不同的事情,并且做得更好。

问题在于我无法控制SP1的数据库,我只需要从其他地方作为第三方客户端进行调用。现在我设法获得了不仅可以查看SP,还可以查看光标类型的权限。我仍然没有看到桌子,但现在有一个更清洁的解决方案:

在另一个数据库中,我被授予了访问权限,现在可以看到这种类型:

type cur_Table1 is ref cursor return Table1%rowtype;

所以在我的数据库中,我现在可以这样做:

mycursor OtherDB.cur_Table1;
myrecord mycursor%rowtype;
...
OtherDB.SP1(mycursor);
fetch mycursor into myrecord;
dbms_output.put_line(myrecord.Column1);

看,我仍然不需要任何访问表,我只看到光标。关键是神奇的%rowtype也适用于游标,而不仅仅是表格。它不适用于sys_refcursor,但它适用于强类型的。鉴于此代码,我不必关心另一方是否有任何更改,我根本不必定义所有列或记录,我只需指定我感兴趣的一列。

我真的很喜欢这种关于Oracle的OOP态度。

答案 2 :(得分:1)

不知道它是否是一个选项,但更好的解决方案是创建一个返回您正在寻找的特定值的函数?这避免了发送额外数据的开销。或者,您可以定义一个游标,其中包含双方都知道的一组已知字段。

相关问题