oracle 函数体中的逗号分隔值
我有以下 oracle 函数,但它不起作用并且出错。我用了询问汤姆 转换逗号分隔值以在
包头中声明的 select * from table1 where col1 in <>
中使用的方法:
TYPE myTableType IS table of varchar2 (255);
包体的一部分:
l_string long default iv_value_with_comma_separated|| ',';
l_data myTableType := myTableType();
n NUMBER;
begin
begin
LOOP
EXIT when l_string is null;
n := instr( l_string, ',' );
l_data.extend;
l_data(l_data.count) := ltrim( rtrim( substr( l_string, 1, n-1 ) ) );
l_string := substr( l_string, n+1 );
END LOOP;
end;
OPEN my_cursor FOR
select * from table_a where column_a in (select * from table (l_data));
CLOSE my_cursor
END;
上面失败,但当我时它工作正常删除
select * from table (l_data)
有人可以告诉我我在这里可能做错了什么吗?
I've got following oracle function but it does not work and errors out. I used Ask Tom's way to convert comma separated values to be used in select * from table1 where col1 in <>
declared in package header:
TYPE myTableType IS table of varchar2 (255);
Part of package body:
l_string long default iv_value_with_comma_separated|| ',';
l_data myTableType := myTableType();
n NUMBER;
begin
begin
LOOP
EXIT when l_string is null;
n := instr( l_string, ',' );
l_data.extend;
l_data(l_data.count) := ltrim( rtrim( substr( l_string, 1, n-1 ) ) );
l_string := substr( l_string, n+1 );
END LOOP;
end;
OPEN my_cursor FOR
select * from table_a where column_a in (select * from table (l_data));
CLOSE my_cursor
END;
above fails but it works fine when I remove
select * from table (l_data)
Can someone please tell me what I might be doing wrong here??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有向我们提供实际的错误,这使得我们更难诊断您的问题。然而,值得一试:
ORA-00902: invalid datatype
您还没有完全按照 Tom 给出的方式实现他的解决方案。具体来说,他将
myTableType
创建为 SQL 类型,而您已在包规范中声明了它。这不是一个微不足道的细节:我们不能在 SQL 语句中使用 PL/SQL 类型。因此例外。因此,从包中删除 MyTableType 的声明并在 SQL 中创建它......
您的 SELECT 语句现在应该可以工作。如果没有,请编辑您的问题以向我们提供确切的错误消息。
编辑
正如您所看到的,PKG1 在规范中声明了 PL/SQL 类型:
在包主体中,您将把 SPLIT() 识别为 Tom Kyte 的解决方案。 GET_RESULTSET() 循环遍历传递的集合并进行 动态 SQL 语句。 GET_RESULTSET_FOR_STR() 是一个调用其他函数的辅助函数,
它在 SQL*Plus 中的工作原理如下:
You don't give us the actual error, which makes it harder for us to diagnose your problem. However, it is worth a punt:
ORA-00902: invalid datatype
You haven't implemented Tom's solution exactly as he gave it. Specifically, he created
myTableType
as a SQL Type whereas you have declared it in the package specification. This is not a trivial detail: we cannot use PL/SQL types in SQL statements. Hence the exception.So, remove the declration of MyTableType from the package and create it in SQL....
You SELECT statement should now work. If it doesn't please edit your question to give us the exact error message.
edit
Here is a kluge. As you can see, PKG1 declares the PL/SQL type in the spec:
In the package body you will recognise SPLIT() as Tom Kyte's solution. GET_RESULTSET() loops through a passed collection and assembles a dynamic SQL statement. GET_RESULTSET_FOR_STR() is a helper funnction which calls both the other functions.
Here is it working in SQL*Plus: