Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an interactive report using pl/sql refcursor

I need to create an interactive report but instead of running a sql statement I need to run a pl/sql statement

-----------------------------------------------------------------------------
USER_ID        Name                 1 java 2 php 3 pl/sql / sql 4 Oracle apex
-------------- -------------------- ------ ----- -------------- -------------
3              Amy brown            N      N     Y              N           
4              Sarah woods          N      N     Y              Y           
2              Johnny paterson      Y      Y     Y              Y           
1              John brown           Y      N     Y              Y   

If this is NOT possible to do in apex, Is there any way of saving the results from that pl/sql statement in a csv format when you run the pl/sql?

In the interactive report, users will not be able to add or remove anything. Its is only for viewing.

Thanks

like image 546
user3057514 Avatar asked Feb 06 '14 12:02

user3057514


1 Answers

As Scott had stated, one option is to create an APEX collection with the refcursor, then create an interactive grid off of the data using the APEX_COLLECTIONS view. I have built an example to demonstrate this.

Create a Pre-Rendering Process at the point Before Header. Using this method, you can add additional logic to not re-populate the data if the collection already exists or so the data is not refreshed every time a user refreshes their page.

Set up of Pre-Rendering Process

Source code of populating the collection:

DECLARE
    l_ref_cur      SYS_REFCURSOR;

    TYPE user_skill_rec IS RECORD
    (
        user_id        NUMBER,
        name           VARCHAR2 (100),
        java           VARCHAR2 (1),
        php            VARCHAR2 (1),
        plsql_sql      VARCHAR2 (1),
        oracle_apex    VARCHAR2 (1)
    );

    l_user_skill   user_skill_rec;

    FUNCTION get_ref_cursor
        RETURN SYS_REFCURSOR
    IS
        l_cur   SYS_REFCURSOR;
    BEGIN
        OPEN l_cur FOR
            SELECT 3               AS user_id,
                   'Amy brown'     AS name,
                   'N'             AS java,
                   'N'             AS php,
                   'Y'             AS plsql_sql,
                   'N'             AS oracle_apex
              FROM DUAL
            UNION ALL
            SELECT 4 AS user_id, 'Sarah woods', 'N', 'N', 'Y', 'Y' FROM DUAL
            UNION ALL
            SELECT 2 AS user_id, 'Johnny paterson', 'Y', 'Y', 'Y', 'Y' FROM DUAL
            UNION ALL
            SELECT 1 AS user_id, 'John brown', 'Y', 'N', 'Y', 'Y' FROM DUAL;

        RETURN l_cur;
    END;
BEGIN
    l_ref_cur := get_ref_cursor;

    apex_collection.create_or_truncate_collection ('USER_SKILLS');

    LOOP
        FETCH l_ref_cur INTO l_user_skill;

        EXIT WHEN l_ref_cur%NOTFOUND;
        apex_collection.add_member (p_collection_name   => 'USER_SKILLS',
                                    p_n001              => l_user_skill.user_id,
                                    p_c001              => l_user_skill.name,
                                    p_c002              => l_user_skill.java,
                                    p_c003              => l_user_skill.php,
                                    p_c004              => l_user_skill.plsql_sql,
                                    p_c005              => l_user_skill.oracle_apex);
    END LOOP;
END;

Then, just create your Interactive Report querying the APEX_COLLECTIONS view with the same collection that had the data populated:

Setup of Interactive Report

Source Query:

select n001, c001, c002, c003, c004, c005
  from apex_collections
 where collection_name = 'USER_SKILLS'
like image 76
EJ Egyed Avatar answered Sep 30 '22 14:09

EJ Egyed