在 PL/SQL 中将表索引转换为简单表的优雅方法,然后循环遍历每个条目
我有两种类型
CREATE OR REPLACE TYPE my_record_type IS OBJECT
(
name varchar2(30)
)
;
CREATE OR REPLACE TYPE my_table_type AS TABLE OF my_record_type
和一个函数
create or replace my_function return my_table_type
is
type my_hash_type is table of my_record_type index by pls_integer;
v_hash my_hash_type;
v_table my_table_type;
i NUMBER;
begin
-- some business logic here
-- transformation part
v_table := my_table_type();
i := v_hash.first;
while i is not null loop
v_table.extend(1);
v_table(v_table.last) := v_hash(i);
i := v_hash.next(i);
end loop;
-- end transformation part
return v_table;
end;
/
10g 中有没有一种优雅的方法可以用类似的东西替换转换部分
v_table = CAST( v_hash as my_table_type )
I have two types
CREATE OR REPLACE TYPE my_record_type IS OBJECT
(
name varchar2(30)
)
;
CREATE OR REPLACE TYPE my_table_type AS TABLE OF my_record_type
and a function
create or replace my_function return my_table_type
is
type my_hash_type is table of my_record_type index by pls_integer;
v_hash my_hash_type;
v_table my_table_type;
i NUMBER;
begin
-- some business logic here
-- transformation part
v_table := my_table_type();
i := v_hash.first;
while i is not null loop
v_table.extend(1);
v_table(v_table.last) := v_hash(i);
i := v_hash.next(i);
end loop;
-- end transformation part
return v_table;
end;
/
Is there an elegant way in 10g to replace the transformation part with something like
v_table = CAST( v_hash as my_table_type )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
SELECT my_record_type(column_value) BULK COLLECT INTO v_table from table(v_hash)
。但为了使用它,您必须在函数外部创建 my_hash_type (作为包规范中的独立类型 OR ,以便 SQL 引擎可见),否则您将收到aPLS-00642:SQL 语句中不允许使用本地集合类型
。看看这里和此处了解更多示例以及
时间差异等(基于此方法 #s 为百分之一秒)
(上述时间试验是变化基于此代码:
因此循环填充速度快了 50%,但时间差仍然很小(这里是过早优化和避免某些事情之间的平衡,因为它可能太长,我建议对真实数据进行时间试验找到最适合的解决方案)。
我能想到的唯一的其他“优雅”解决方案是 TREAT,但您会注意到它需要一个必须位于对象类型上的子类型/超类型解决方案(我无法让它在 Varray/Assoc 上工作) 。数组类型——希望我错了!)
You may use the
SELECT my_record_type(column_value) BULK COLLECT INTO v_table from table(v_hash)
. But in order to use this, you will have to create my_hash_type outside of a function (either as a stand along type OR in a Package Specification so it will be visible to the SQL Engine) otherwise you will receive aPLS-00642: local collection types not allowed in SQL statements
.have a look here and here for some more examples and whatnot
timing differences (based on this methodology #s are in hundreths of a second)
(the above time trials were variations based on this code:
Thus the loop fill is 50% faster, but the time difference is still minuscule (here is the balance between premature optimization and avoiding something because it may be too long, I would recommend doing time trials on your real data to find the solution that best fits).
The only other 'elegant' solution I can think of is TREAT, but you'll notice it requires a subtype/supertype solution that must be on an object type (I couldn't get it to work on an Varray/Assoc. Array type -- hopefully I'm wrong!)