Oracle - 从SYS_REFCURSOR中选择的pl sql

时间:2010-04-04 19:44:11

标签: sql oracle plsql

我有一个函数返回一个SYS_REFCURSOR,它有一行但多列。我想要做的是能够使用SYS_REFCURSOR中返回的列值来生成具有嵌套子查询的SQL查询。其他想法,如类型等将不胜感激。下面的代码是我即时编写的,尚未经过语法验证。

--Oracle function
CREATE DummyFunction(dummyValue AS NUMBER) RETURN SYS_REFCURSOR
IS
  RETURN_DATA SYS_REFCURSOR;
BEGIN
  OPEN RETURN_DATA
  SELECT
    TO_CHAR(dummyValue) || 'A' AS ColumnA
    ,TO_CHAR(dummyValue) || 'B' AS ColumnB
  FROM
    DUAL;

  RETURN RETURN_DATA;
END;

--sample query with sub-queries; does not work
SELECT
  SELECT ColumnA FROM DummyFunction(1) FROM DUAL AS ColumnA
  ,SELECT ColumnB FROM DummyFunction(1) FROM DUAL AS ColumnB
FROM
  DUAL;

1 个答案:

答案 0 :(得分:5)

SYS_REFCURSOR不适用于预期用途 - 您需要创建Oracle TYPE:

CREATE TYPE your_type IS OBJECT (
  ColumnA VARCHAR2(100),
  ColumnB VARCHAR2(100)
)

更新您的功能:

CREATE DummyFunction(dummyValue AS NUMBER) 
RETURN your_type
IS

BEGIN

  INSERT INTO your_type
  SELECT TO_CHAR(dummyValue) || 'A' AS ColumnA,
         TO_CHAR(dummyValue) || 'B' AS ColumnB
    FROM DUAL;

  RETURN your_type;

END;

然后你可以使用:

SELECT (SELECT ColumnA FROM table(DummyFunction(1))) AS ColumnA, 
       (SELECT ColumnB FROM table(DummyFunction(1))) AS ColumnB
  FROM DUAL

示例过于复杂 - 您只需要使用:

SELECT x.columna,
       x.columnb
  FROM table(DummyFunction(1)) x