PL/SQL 包类型与模式类型
我想做的是以下内容:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要使用使用该类型的 SQL select 语句,那么它必须是 SQL 类型。您不能将 PL/SQL 类型返回到 SQL。
另一个可能有效的选择是创建一个 for 循环并填充数组。
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.