在 IN 子句中使用 Oracle 表类型 - 编译失败

发布于 2024-08-18 01:09:40 字数 862 浏览 6 评论 0原文

只是试图将光标返回到我指定的 id 处。

CREATE OR REPLACE PACKAGE some_package AS

  TYPE t_cursor IS REF CURSOR;
  TYPE t_id_table IS TABLE OF NVARCHAR(38) INDEX BY PLS_INTEGER;
  
  PROCEDURE someentity_select(
    p_ids     IN  t_id_table, 
    p_results OUT t_cursor);
  
END;

CREATE OR REPLACE PACKAGE BODY some_package AS
  
  PROCEDURE someentity_select(
    p_ids     IN  t_guid_table, 
    p_results OUT t_cursor)
  IS
  BEGIN
  
    OPEN p_results FOR 
      SELECT * 
      FROM someschema.someentity 
      WHERE id IN (SELECT column_value FROM TABLE(p_ids)); - fails here
      
  END;

END;

注意:someschema.someentity.id 是 NVARCHAR2(38)

PL/SQL: ORA-00382: expression is of wrong type
PL/SQL: ORA-22905: cannot access rows from a non-nested table item

我哪里出错了?

Simply trying to get a cursor back for the ids that I specify.

CREATE OR REPLACE PACKAGE some_package AS

  TYPE t_cursor IS REF CURSOR;
  TYPE t_id_table IS TABLE OF NVARCHAR(38) INDEX BY PLS_INTEGER;
  
  PROCEDURE someentity_select(
    p_ids     IN  t_id_table, 
    p_results OUT t_cursor);
  
END;

CREATE OR REPLACE PACKAGE BODY some_package AS
  
  PROCEDURE someentity_select(
    p_ids     IN  t_guid_table, 
    p_results OUT t_cursor)
  IS
  BEGIN
  
    OPEN p_results FOR 
      SELECT * 
      FROM someschema.someentity 
      WHERE id IN (SELECT column_value FROM TABLE(p_ids)); - fails here
      
  END;

END;

Note: someschema.someentity.id is a NVARCHAR2(38)

PL/SQL: ORA-00382: expression is of wrong type
PL/SQL: ORA-22905: cannot access rows from a non-nested table item

Where am I going wrong?

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

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

发布评论

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

评论(2

久而酒知 2024-08-25 01:09:40

在 12.2 之前的 Oracle 版本中,您只能从通过 CREATE TYPE 语句在数据库中定义的集合类型中进行 SELECT,不能是关联数组:

CREATE TYPE t_id_table IS TABLE OF NVARCHAR(38);

CREATE OR REPLACE PACKAGE some_package AS

  PROCEDURE someentity_select(
    p_ids     IN  t_guid_table, 
    p_results OUT SYS_REFCURSOR);

END;

CREATE OR REPLACE PACKAGE BODY some_package AS

  PROCEDURE someentity_select(
    p_ids     IN  t_guid_table, 
    p_results OUT SYS_REFCURSOR)
  IS
  BEGIN

    OPEN p_results FOR 
      SELECT * 
      FROM someschema.someentity 
      WHERE id IN (SELECT column_value FROM TABLE(p_ids));

  END;

END;

In Oracle versions prior to 12.2 you can only SELECT from a collection type that is defined in the database via a CREATE TYPE statement, not an associative array:

CREATE TYPE t_id_table IS TABLE OF NVARCHAR(38);

CREATE OR REPLACE PACKAGE some_package AS

  PROCEDURE someentity_select(
    p_ids     IN  t_guid_table, 
    p_results OUT SYS_REFCURSOR);

END;

CREATE OR REPLACE PACKAGE BODY some_package AS

  PROCEDURE someentity_select(
    p_ids     IN  t_guid_table, 
    p_results OUT SYS_REFCURSOR)
  IS
  BEGIN

    OPEN p_results FOR 
      SELECT * 
      FROM someschema.someentity 
      WHERE id IN (SELECT column_value FROM TABLE(p_ids));

  END;

END;
我还不会笑 2024-08-25 01:09:40

这是一个索引表,它是 PL/SQL 类型。

只能在Oracle的SQL引擎中使用SQL类型。或者 PL/SQL 类型,Oracle 可以将其修改为看起来像 SQL 类型。

您可以拥有一个简单的类似数组的集合并将其用作结果。 (无索引)

type TGuidList is table of NVarchar(38);

但是最好的兼容性和稳定性,可以通过将其声明为全局 SQL 类型并在包内使用它来获得:

create type TGuidList is table of NVarchar(38);

编辑:您不需要 NVarChar 作为 GUID,对吗?一个好的 ol' VarChar 应该可以很好地完成这个任务。

This is an index-by table, which is a PL/SQL type.

You can only use SQL types in the SQL engine of Oracle. Or PL/SQL types, that Oracle can hack around to look like SQL types.

You can have a simple array-like collection and use it as a result. (no index by)

type TGuidList is table of NVarchar(38);

But the best compatibility and stability, you get by declaring it as a global SQL type and use that inside your package:

create type TGuidList is table of NVarchar(38);

Edit: You will not need an NVarChar for a GUID, will you? A good ol' VarChar should do the trick just fine.

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