PL/SQL 包类型与模式类型

发布于 2024-11-19 11:03:25 字数 1166 浏览 4 评论 0原文

我想做的是以下内容:

create or replace
package MyPackage
as
    type string_list_t is table of varchar2(32767);

    function GetPrimaryKeys    ( p_table_name varchar2, p_owner varchar2 )
        return string_list_t;
end MyPackage;
/

create or replace
package body MyPackage as

function GetPrimaryKeys ( p_table_name varchar2, p_owner varchar2 )
return string_list_t
is
    pk_descr string_list_t;
begin
    select cast( multiset (
        select cols.column_name
          from all_constraints cons, all_cons_columns cols
          where cols.table_name = p_table_name
          and cons.constraint_type = 'P'
          and cons.constraint_name = cols.constraint_name
          and cols.owner = p_owner
          and cons.owner = p_owner
    ) as string_list_t) into pk_descr from dual; -- error refers to string_list_t

     return pk_descr;
end;

end MyPackage;

但它无法编译:

错误(16,14):PL/SQL:ORA-00902:无效数据类型

当我在包外部定义 string_list_t 时,

create or replace
type string_list_t is table of varchar2(32767);

它会按预期进行编译和工作。这里有什么区别以及如何使用包中定义的类型正确执行此操作?

What I'm trying to do is the following:

create or replace
package MyPackage
as
    type string_list_t is table of varchar2(32767);

    function GetPrimaryKeys    ( p_table_name varchar2, p_owner varchar2 )
        return string_list_t;
end MyPackage;
/

create or replace
package body MyPackage as

function GetPrimaryKeys ( p_table_name varchar2, p_owner varchar2 )
return string_list_t
is
    pk_descr string_list_t;
begin
    select cast( multiset (
        select cols.column_name
          from all_constraints cons, all_cons_columns cols
          where cols.table_name = p_table_name
          and cons.constraint_type = 'P'
          and cons.constraint_name = cols.constraint_name
          and cols.owner = p_owner
          and cons.owner = p_owner
    ) as string_list_t) into pk_descr from dual; -- error refers to string_list_t

     return pk_descr;
end;

end MyPackage;

But it doesn't compile:

Error(16,14): PL/SQL: ORA-00902: invalid datatype

When I define string_list_t outside of the package as

create or replace
type string_list_t is table of varchar2(32767);

it compiles and works as expected. What makes the difference here and how do I do it correctly with the type defined in package?

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

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

发布评论

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

评论(1

孤独陪着我 2024-11-26 11:03:25

如果您要使用使用该类型的 SQL select 语句,那么它必须是 SQL 类型。您不能将 PL/SQL 类型返回到 SQL。

另一个可能有效的选择是创建一个 for 循环并填充数组。

CREATE OR REPLACE PACKAGE BODY MyPackage as

  function GetPrimaryKeys ( p_table_name varchar2, p_owner varchar2 ) return string_list_t
  is
    pk_descr string_list_t;
    --iCounter  number:=1;
  begin

    /*------------------------------
     -       new version                 
     ------------------------------*/
    SELECT cols.column_name
      BULK COLLECT INTO pk_descr
    FROM   all_constraints cons, 
           all_cons_columns cols
    WHERE  cols.table_name = p_table_name
      AND  cons.constraint_type = 'P'
      AND  cons.constraint_name = cols.constraint_name
      AND  cols.owner = p_owner
      AND  cons.owner = p_owner;

    /* old version */
    FOR rec in (select cols.column_name
                from   all_constraints cons, 
                       all_cons_columns cols
                where  cols.table_name = p_table_name
                  and  cons.constraint_type = 'P'
                  and cons.constraint_name = cols.constraint_name
                  and cols.owner = p_owner
                  and cons.owner = p_owner) 
    LOOP

      pk_descr(iCounter):= rec.column_name;
      iCounter:= iCounter+1;
    END LOOP;


    RETURN pk_descr;
 END;

END MyPackage; 

If you are going to use a SQL select statement that uses the type, then it must be a SQL type. You cannot return a PL/SQL type to SQL.

Another option that might work is to create a for loop and populate the array.

CREATE OR REPLACE PACKAGE BODY MyPackage as

  function GetPrimaryKeys ( p_table_name varchar2, p_owner varchar2 ) return string_list_t
  is
    pk_descr string_list_t;
    --iCounter  number:=1;
  begin

    /*------------------------------
     -       new version                 
     ------------------------------*/
    SELECT cols.column_name
      BULK COLLECT INTO pk_descr
    FROM   all_constraints cons, 
           all_cons_columns cols
    WHERE  cols.table_name = p_table_name
      AND  cons.constraint_type = 'P'
      AND  cons.constraint_name = cols.constraint_name
      AND  cols.owner = p_owner
      AND  cons.owner = p_owner;

    /* old version */
    FOR rec in (select cols.column_name
                from   all_constraints cons, 
                       all_cons_columns cols
                where  cols.table_name = p_table_name
                  and  cons.constraint_type = 'P'
                  and cons.constraint_name = cols.constraint_name
                  and cols.owner = p_owner
                  and cons.owner = p_owner) 
    LOOP

      pk_descr(iCounter):= rec.column_name;
      iCounter:= iCounter+1;
    END LOOP;


    RETURN pk_descr;
 END;

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