使用Oracle:是否可以在数据透视子句中使用“动态”创建的变量?

时间:2018-07-06 09:52:13

标签: oracle variables pivot

我正在尝试创建从变量获取参数的数据透视子句。

我有一些测试代码,例如:

exp

这很好用。然后我将变量EXAM_IDS声明为:

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
;

我很确定变量EXAM_IDS现在具有在数据透视子句(?)中使用的字符串,但是我不知道如何将这两个变量结合在一起:

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;

这不起作用。有没有办法做到这一点,还是我应该只运行两个单独的SQL查询?

有关此设置(如我的课程)的更多信息,请访问Using Oracle combine three tables to one with PIVOT

1 个答案:

答案 0 :(得分:1)

对于12c及更高版本,您可以通过为动态DBMS_SQL.RETURN_RESULT查询打开REFCURSOR使用PIVOT

我为(+)删除了臭名昭著的left join语法,始终使用ANSI join语法。

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;
/

对于11g,您可以使用绑定变量和print命令(当以脚本(F5)身份运行时在sqlplus和sql developer / Toad中有效)

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