2016-06-17 43 views
0

如何查看返回記錄數據類型的函數返回的值? 這裏是查詢在oracle中返回記錄的函數

TYPE employee_record_info IS RECORD (
employee_id NUMBER, 
employee_name VARCHAR2(100), 
manager_id NUMBER, 
location VARCHAR2(100) 
); 

FUNCTION function1(in_employee_id NUMBER) RETURN employee_record_info AS 
    l_record employee_record_info; 
    BEGIN 
     SELECT employee_id, employee_name, manager_id, location 
     INTO 
     l_record 
     FROM all_employees where employee_id = in_employee_id;  
    RETURN l_record; 
    END function1; 

我試圖

select * from table(function1(123)); 

select function1(123) from dual; 

我在這兩種情況下獲得了無效的錯誤類型的功能?有沒有辦法獲得這些值。 我只需要這個來測試我的功能,這不適用於任何代碼。

請在函數返回數組的情況下也需要一些幫助。

謝謝。

+2

向我們展示您的函數定義 –

+0

編輯了問題 – ravi

回答

4

你的代碼是pl/sql,你將無法使用SQL進行選擇。您可以重寫它以使其適用於SQL選擇,如下所示:

SQL> create or replace type emp_rec_typ as object(
employee_id NUMBER, 
employee_name VARCHAR2(100), 
manager_id NUMBER, 
location VARCHAR2(100) 
); 
Type created. 
SQL> create or replace type emp_tab_typ as table of emp_rec_typ; 
Type created. 
SQL> create or replace function get_emps 
return emp_tab_typ 
as 
    emps emp_tab_typ := emp_tab_typ(); 
begin 
    select emp_rec_typ(x.employee_id, x.employee_name, x.manager_id, x.location) 
    bulk collect into emps 
    from (
     select 1 as employee_id, 'Joe Blow' as employee_name, 1 as manager_id, 'Some Place' as location from dual 
     union all 
     select 2 as employee_id, 'Jane Doe' as employee_name, 1 as manager_id, 'Some Other Place' as location from dual 
     union all 
     select 3 as employee_id, 'Fred Smith' as employee_name, 2 as manager_id, 'Some Strange Place' as location from dual 
    ) x; 

    return emps; 

end; 
Function created. 
SQL> select * from table(get_emps); 

EMPLOYEE_ID EMPLOYEE_NAME MANAGER_ID LOCATION 
1 'Joe Blow' 1 'Some Place' 
2 'Jane Doe' 1 'Some Other Place' 
3 'Fred Smith' 2 'Some Strange Place'