Oracle - pl sql 从 SYS_REFCURSOR 中选择

发布于 2024-08-27 20:15:41 字数 622 浏览 4 评论 0原文

我有一个返回 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;

I have a function that returns a SYS_REFCURSOR that has a single row but multiple columns. What I'm looking to do is to be able to have a SQL query that has nested sub-queries using the column values returned in the SYS_REFCURSOR. Alternative ideas such as types, etc would be appreciated. Code below is me writing on-the-fly and hasn't been validated for syntax.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清眉祭 2024-09-03 20:15:41

SYS_REFCURSOR 不适用于预期用途 - 您需要创建一个 Oracle 类型:

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

A SYS_REFCURSOR won't work for the intended use - you need to create an Oracle TYPE:

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

Update your function:

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;

Then you can use:

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

The example is overcomplicated - all you need to use is:

SELECT x.columna,
       x.columnb
  FROM table(DummyFunction(1)) x
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文