Using Oracle: Can I use 'dynamically' crea

2019-08-12 15:29发布

I'm trying to create pivot clause that gets it parameters from variable.

I have some test code like:

SELECT * FROM (
SELECT U.USER_ID, U.USER_NAME, E.EXAM_ID, EU.EXAM_DATE
FROM USERS U, EXAMS E, EXAM_USER EU
WHERE U.USER_ID = EU.USER_ID(+)
AND E.EXAM_ID(+) = EU.EXAM_ID
ORDER BY U.USER_ID
  )
  PIVOT (MAX(EXAM_DATE) FOR EXAM_ID IN ('3' AS "exam 3",'2' AS "exam 2",'1' AS "exam 1"))
  order by 1
;

This works just fine. Then I declared the variable EXAM_IDS like:

DECLARE
  EXAM_IDS VARCHAR2 (255);
BEGIN
  SELECT LISTAGG('''' || EXAM_ID || ''' AS "' || EXAM_NAME || '"', ',')
  WITHIN GROUP (ORDER BY EXAM_ID DESC) 
  INTO EXAM_IDS
  FROM EXAMS;
END;

I'm pretty sure the variable EXAM_IDS has now a string as used in pivot clause(?) but I don know how to combine these two:

DECLARE
  EXAM_IDS VARCHAR2 (255);
BEGIN
  SELECT LISTAGG('''' || EXAM_ID || ''' AS "' || EXAM_NAME || '"', ',')
  WITHIN GROUP (ORDER BY EXAM_ID DESC) 
  INTO EXAM_IDS
  FROM EXAMS;
END;

SELECT * FROM (
SELECT U.USER_ID, U.USER_NAME, E.EXAM_ID, EU.EXAM_DATE
FROM USERS U, EXAMS E, EXAM_USER EU
WHERE U.USER_ID = EU.USER_ID(+)
AND E.EXAM_ID(+) = EU.EXAM_ID
ORDER BY U.USER_ID
  )
  PIVOT (MAX(EXAM_DATE) FOR EXAM_ID IN (' || EXAM_IDS || '))
  order by 1
;

And this does not work. Is there a way to do this or should I just run two separate SQL queries?

More info about this setup (like my classes) can be found from Using Oracle combine three tables to one with PIVOT

1条回答
Melony?
2楼-- · 2019-08-12 15:43

For 12c and above, you may use DBMS_SQL.RETURN_RESULT by opening a REFCURSOR for the dynamic PIVOT query.

I have removed the notorious (+) syntax for left join, always use the ANSI join syntax.

DECLARE
    exam_ids   VARCHAR2(255);
    x          SYS_REFCURSOR;
BEGIN
    SELECT
        LISTAGG(''''
                  || exam_id
                  || ''' AS "'
                  || exam_name
                  || '"',',') WITHIN GROUP(
            ORDER BY
                exam_id DESC
        )
    INTO exam_ids
    FROM
        exam;

    OPEN x FOR 'SELECT
        *
               FROM
        (
            SELECT
                u.user_id,
                u.user_name,
                e.exam_id,
                eu.exam_date
            FROM
                users u
                LEFT JOIN exam_user eu ON u.user_id = eu.user_id
                LEFT JOIN exam e ON e.exam_id = eu.exam_id
            ORDER BY
                u.user_id
        )
            PIVOT ( MAX ( exam_date )
                FOR exam_id
                IN ( ' || EXAM_IDS || ' )
            )
    ORDER BY
        1';

    dbms_sql.return_result(x);
END;
/

For 11g, you may use a bind variable and print command ( works in sqlplus and in sql developer/Toad when run as script (F5))

variable x REFCURSOR  -- bind variable declared.
DECLARE
    ..   -- no need to declare sys_refcursor
BEGIN
     ..

    OPEN :x FOR 'SELECT . --note the change with colon
        *
               FROM
        (
            SELECT
            ..

END;
/


print x

enter image description here

查看更多
登录 后发表回答