查询在oracle中返回记录的函数

时间:2016-06-17 19:42:33

标签: sql oracle plsql

如何查看返回记录数据类型的函数返回的值? 这是函数

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;

我在两种情况下都收到无效的类型错误?有没有办法获得这些价值观。 我只需要这个来测试我的功能,这不适用于任何代码。

在函数返回数组的情况下也需要一些帮助。

感谢。

1 个答案:

答案 0 :(得分: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'