且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Oracle PL/SQL 中填充我自己创建的数据类型的变量?

更新时间:2022-12-04 08:16:29

"我要的是在PL/SQL期间填充它代码本身,带有 INSERT 语句"

"I want is to fill it during the PL/SQL code itself, with INSERT statements"

这就像填充任何其他 PL/SQL 变量:我们必须使用 INTO.只是因为我们要填充多行,所以我们需要使用 BULK COLLECT 语法.

It's like populating any other PL/SQL variable: we have to use INTO. Only because we're populating multiple rows we need to use the BULK COLLECT syntax.

declare
    l_array your_nested_table_type;
begin
    select col1
    bulk collect into l_array
    from t72;
end;
/

如果结果集返回大量记录,***在循环内使用 LIMIT 子句.这是因为 PL/SQL 集合——就像所有其他 PL/SQL 变量一样——保存在会话内存中.所以我们不希望阵列变得太大,否则它可能会炸毁 PGA.找出更多.

If the result set returns a lot of records it is a good idea to use the LIMIT clause inside a loop. This is because PL/SQL collections - just like every other PL/SQL variable - are held in session memory. So we don't want the array to get too big, otherwise it might blow the PGA. Find out more.

编辑

我编辑了问题以澄清特别是我想要的"

"I edited the question to clarify specifically what I want"

sigh 对字符串进行标记是一个完全不同的问题.我之前在两个 SO 线程中发布了解决方案.如果您使用的是 9i 或更早版本,请使用这种方法.否则使用 这个正则表达式解决方案(实际上这将字符串拆分为数字标记,但很容易转换为字符).

sigh Tokenizing a string is an altogether different issue. I have previously posted solutions in two SO threads. If you're using 9i or earlier use this approach. Otherwise use this regex solution (actually this splits the string into numeric tokens, but it is easy enough to convert to characters).

编辑 2

"我只想能够使用输入内部"(在 Stored过程)通过向其添加值.是这是我可以用第一个做的链接?"

"I only want to be able to use the type "internally" (within the Stored Procedure) by adding values to it. Is this something I can do with the first link?"

当然.为什么不呢?

SQL> declare
  2      local_array tok_tbl;
  3  begin
  4      local_array := parser.my_parse('Keith Pellig,Peter Wakeman,Ted Bentley,Eleanor Stevens');
  5      local_array.extend();
  6      local_array(5) := 'Reese Verrick';
  7      for i in local_array.first()..local_array.last()
  8      loop
  9          dbms_output.put_line(local_array(i));
 10      end loop;
 11  end;
 12  /
Keith Pellig
Peter Wakeman
Ted Bentley
Eleanor Stevens
Reese Verrick

PL/SQL procedure successfully completed.

SQL>

这里我重新使用了我的 SQL 类型,但如果 TOK_TBL 在 PL/SQL 包中声明,它也能正常工作.

Here I have re-used my SQL type, but it would work just as well if TOK_TBL were declared in the PL/SQL package instead.