2014-10-10 20 views
0

我想要獲取所有列名稱my_table,並且想使用my_fn作爲字符串。例如,假設my_table具有名爲「年」的列,「月」,「日」。使用all_tab_cols表獲取此列名稱,並將其存儲到tmp集合中。使用for循環,我想這些列名稱爲「月,年,日」使用my_fn。目前,我收到以下錯誤。PL/SQL - 無法訪問非嵌套表項中的行

錯誤日誌 - 這是我得到的錯誤

SQL Error: ORA-22905: cannot access rows from a non-nested table item 
22905. 00000 - "cannot access rows from a non-nested table item" 
*Cause: attempt to access rows of an item whose type is not known at 
      parse time or that is not of a nested table type 
*Action: use CAST to cast the item to a nested table type  

SQL代碼 - 這是我的代碼。

CREATE OR REPLACE TYPE col_array as table of varchar2(1000); 
/
CREATE OR REPLACE FUNCTION my_fn (
    input_1 IN VARCHAR2, 
    input_2 IN VARCHAR2, 
    input_3 IN VARCHAR2) 
    RETURN varchar2 AS 
     tmp col_array; 
     txt varchar2(1000); 

    BEGIN 
    SELECT column_name bulk collect into tmp 
    FROM all_tab_cols 
    where owner = 'me' and table_name ='my_table'; 

     for i in 1..tmp.count loop 
     txt := txt || to_char(tmp(i)) || ','; 
     end loop; 

    //txt := 'wow'; 

    RETURN txt; 

    END my_fn; 
/
SELECT * FROM TABLE(my_fn('','','')); 

我也嘗試了以下簡單的代碼,但仍然無法正常工作。我可能需要關於如何使用CAST功能:(

BEGIN   
    txt := 'wow'; //or txt := CAST('wow' as varchar2);   
RETURN txt; 

你的幫助會非常感激!

+0

什麼的Oracle版本是您使用來獲取值? – Mureinik 2014-10-10 13:28:03

回答

6

你的函數返回一個字符串更多的知識不是一個集合,你可以使用

SELECT my_fn('','','') FROM dual; 

+0

OH MY GOD !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!! 非常感謝!!!!!!! – Adrian 2014-10-10 13:34:42