2012-01-16 21 views
1

我需要從Oracle過程返回一個遊標,遊標必須包含RECORD類型。當RECORD很簡單時,我知道如何解決這個問題,但是如何解決這個問題?NESTED RECORDS如何從Oracle過程返回填充了NESTED RECORD類型的遊標

目前的代碼簡單的記錄工作塊:

-- create package with a RECORD type 
create or replace package pkg as 
    -- a record contains only one simple attribute 
    type t_rec is RECORD (
     simple_attr number 
    ); 
end; 
/

-- create a testing procedure 
-- it returns a cursor populated with pkg.t_rec records 
create or replace procedure test_it(ret OUT SYS_REFCURSOR) is 
    type cur_t is ref cursor return pkg.t_rec; 
    cur cur_t; 
begin 
    -- this is critical; it is easy to populate simple RECORD type, 
    -- because select result is mapped transparently to the RECORD elements 
    open cur for select 1 from dual; 
    ret := cur; -- assign the cursor to the OUT parameter 
end; 
/

-- and now test it 
-- it will print one number (1) to the output 
declare 
    refcur SYS_REFCURSOR; 
    r pkg.t_rec; 
begin 
    -- call a procedure to initialize cursor 
    test_it(refcur); 
    -- print out cursor elements 
    loop 
     fetch refcur into r; 
     exit when refcur%notfound; 
     dbms_output.put_line(r.simple_attr); 
    end loop; 
    close refcur; 
end; 
/

你能告訴我,如何當一個記錄t_rec包含嵌套RECORD它可以做?

修改示例如下因素的方法:

-- create package with a NESTED RECORD type 
create or replace package pkg as 
    type t_rec_nested is RECORD (
     nested_attr number 
    ); 
    -- a record with NESTED RECORD 
    type t_rec is RECORD (
     simple_attr number, 
     nested_rec t_rec_nested 
    ); 
end; 
/

create or replace procedure test_it(ret OUT SYS_REFCURSOR) is 
    type cur_t is ref cursor return pkg.t_rec; 
    cur cur_t; 
begin 
    -- how to populate a result? 
    open cur for ???? 
    ret := cur; 
end; 
/

的問題是如何修改test_it過程來填充光標? 我花了很多時間搜索解決方案,我會感謝任何幫助。

回答

1

我不認爲這是可能的,因爲RECORDs是PL/SQL數據類型。你可以做一個對象來做同樣的事情。如果您向下滾動至this link的底部(或者在頁面上搜索「使用包含對象的記錄更新行:示例」),您將看到如何處理這些內容。

+1

是的!我不明白我怎麼能錯過這樣一個明顯的解決方案。非常感謝你。 – giorge 2012-01-17 13:54:50

相關問題