简体   繁体   中英

Use parameter from function in cursor where clause and cursor rowtype

I am trying to create a pl/sql function that accepts a value. A cursor uses that value in a where clause and the function should return the first row of the cursor.

This is what I am trying to get to work.

create or replace package pkg_test_slot as

cursor c_test_slotis 
select * from (select person_uid, test.test, rownum r from test)
pivot(max(test) test_code for r in (1,2,3,4,5));

function f_test_getter (p_entity_uid number) return c_test_slot%rowtype;

end;

Something like this

create or replace package body p_test is
  function get_rows(pll_limit in number) return sys_refcursor is
    lcur_rec sys_refcursor;
  begin
    open lcur_rec for
      select rownum rw, dum
        from (select 1 dum from dual group by cube(1, 2, 3, 4, 5))
       where rownum < pll_limit;

    return lcur_rec;
  end get_rows;

  procedure prc_get_data is
    lcur sys_refcursor;
  begin
    lcur := get_rows(10);
  end prc_get_data;
end p_test;

You could implement F_TEST_GETTER as:

CREATE OR REPLACE PACKAGE PKG_TEST_SLOT AS
  CURSOR C_TEST_SLOT(pEUID IN NUMBER) IS 
    SELECT PERSON_UID, TEST.TEST, ROWNUM R
              FROM TEST t
              WHERE t.ENTITY_UID = pEUID;

  FUNCTION F_TEST_GETTER (P_ENTITY_UID NUMBER)
    RETURN PKG_TEST_SLOT.C_TEST_SLOT%ROWTYPE;
END PKT_TEST_SLOT;

CREATE OR REPLACE PACKAGE BODY PKG_TEST_SLOT AS
  FUNCTION F_TEST_GETTER(P_ENTITY_UID NUMBER)
    RETURN PKG_TEST_SLOT.C_TEST_SLOT%ROWTYPE
  AS
    aRow PKG_TEST_SLOT.C_TEST_SLOT%ROWTYPE;
  BEGIN
    OPEN C_TEST_SLOT(P_ENTITY_UID);

    FETCH C_TEST_SLOT INTO aRow;

    CLOSE C_TEST_SLOT;

    RETURN aRow;
  END PKG_TEST_SLOT;
END PKG_TEST_SLOT;

Note that I've simplified the definition of C_TEST_SLOT a bit, added the parameter, and shown how it's used in the function. Hopefully that gives you some ideas. Share and enjoy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM