Oracle函数返回列表

时间:2018-08-02 15:08:00

标签: oracle plsql

我正在尝试开发一个PLSQL函数,该函数输出一个雇员姓名列表,然后可以通过另一个脚本运行它。我不太明白。我对PLSQL还是很陌生,主要用于在Python中构建函数,因此我可能会以错误的方式思考。最后,我想在我正在编写的另一个脚本中使用此输出。

基准脚本:

   SELECT EMPLOYEE FROM (
   SELECT ID, EMPLOYEE, ROLE, STARTDATE,
    ROW_NUMBER() OVER (PARTITION BY EMPLOYEE ORDER BY STARTDATE DESC, ID DESC) RN
    FROM (
    SELECT DISTINCT E.EMPLOYEE EMPLOYEE,
    E.ID ID,
    LR.DESCRIPTION ROLE, 
    ROLE_START_DATE STARTDATE
    FROM EMPLOYEES E
    JOIN ROLES R ON E.EMPLOYEE_ID = R.EMPLOYEE_ID
    JOIN LU_ROLES LR ON R.ROLE_ID = LR.ROLE_ID
    WHERE ROLE_START_DATE <= DATE '2017-12-03'))
    WHERE RN = 1

我尝试编写PLSQL函数:

CREATE FUNCTION get_employees(EMPLOYEE IN VARCHAR2)
    RETURN VARCHAR2
    IS EMPLOYEE_LIST;
    BEGIN
        SELECT EMPLOYEE FROM (
        SELECT ID, EMPLOYEE, ROLE, STARTDATE,
        ROW_NUMBER() OVER (PARTITION BY EMPLOYEE ORDER BY STARTDATE DESC, ID DESC) RN
        FROM (
        SELECT DISTINCT E.EMPLOYEE EMPLOYEE,
        E.ID ID,
        LR.DESCRIPTION ROLE, 
        ROLE_START_DATE STARTDATE
        FROM EMPLOYEES E
        JOIN ROLES R ON E.EMPLOYEE_ID = R.EMPLOYEE_ID
        JOIN LU_ROLES LR ON R.ROLE_ID = LR.ROLE_ID
        WHERE ROLE_START_DATE <= DATE '2017-12-03'))
        WHERE RN = 1
    RETURN EMPLOYEE_LIST
    END;

我知道我缺少某些语法,我只是不知道什么以及为什么...我现在正在阅读文档以尝试理解这一点。大家提供的任何帮助将不胜感激!

谢谢!

2 个答案:

答案 0 :(得分:1)

在没有表或样本数据的情况下,这可能只是个猜测,但是“固定”版本可能是这样的:

create or replace type short_string_tt as table of varchar2(100)
/

create or replace function get_employees
    ( p_role_date_cutoff roles.role_start_date%type )
    return short_string_tt
as
    l_employee_list short_string_tt;
begin
    select employee bulk collect into l_employee_list
    from   ( select employee
                  , row_number() over(partition by employee order by role_start_date desc, id desc) rn
             from   ( select distinct e.employee, e.id, lr.description, role_start_date
                      from   employees e
                             join roles r
                                  on  r.employee_id = e.id
                             join lu_roles lr
                                  on  lr.role_id = r.role_id
                      where  role_start_date <= p_role_date_cutoff )
           )
    where  rn = 1;

    return l_employee_list;
end;
/

如果返回的行数可能很大,那么您可能会考虑将其设为pipelined function,因为这些流在获取时就返回,而不是在返回任何内容之前在内存中构建整个集合。 / p>

答案 1 :(得分:0)

尝试一下是否可行?

CREATE FUNCTION get_employees(EMPLOYEE IN VARCHAR2)
    RETURN VARCHAR2
    IS EMPLOYEE_LIST VARCHAR2(200);
    BEGIN
        SELECT EMPLOYEE into EMPLOYEE_LIST FROM (
        SELECT ID, EMPLOYEE, ROLE, STARTDATE,
        ROW_NUMBER() OVER (PARTITION BY EMPLOYEE ORDER BY STARTDATE DESC, ID DESC) RN
        FROM (
        SELECT DISTINCT EMPLOYEE EMPLOYEE,
        E.ID ID,
        LR.DESCRIPTION ROLE, 
        ROLE_START_DATE STARTDATE
        FROM EMPLOYEES E
        JOIN ROLES R ON E.EMPLOYEE_ID = R.EMPLOYEE_ID
        JOIN LU_ROLES LR ON R.ROLE_ID = LR.ROLE_ID
        WHERE ROLE_START_DATE <= DATE '2017-12-03'))
        WHERE RN = 1;
    RETURN EMPLOYEE_LIST;
    END;