存储过程中的Reg_exp需要“进入”

时间:2018-03-12 20:34:01

标签: sql regex oracle regexp-substr

我在Oracle存储过程中有这个声明

select regexp_substr('hello,world', '[^(,|;|\s|&)]+', 1, level)
from dual
connect by regexp_substr('hello,world', '[^(,|;|\s|&)]+', 1, level) is not null;

然而,编译器抱怨我需要一个“into”子句。我试过(在我做之前就知道了)并且它不起作用,因为reg_exp返回多个值。

有人可以帮忙吗?谢谢!

2 个答案:

答案 0 :(得分:1)

您可以cursors使用multiple-row返回:

SQL> set serveroutput on;
SQL> declare
      v_abc  varchar2(1500); 
  begin
  for c in ( select regexp_substr('hello,world', '[^(,|;|\s|&)]+', 1, level) abc
               from dual
             connect by regexp_substr('hello,world', '[^(,|;|\s|&)]+', 1, level) is not null )
  loop
   v_abc := c.abc;
   dbms_output.put_line(v_abc);
  end loop;
end;

答案 1 :(得分:0)

您必须使用集合来存储多行结果。

摘自documentation

以下示例演示如何使用SELECT INTO语句将整行查询到PL / SQL记录集合中:

DECLARE
   TYPE first_typ IS TABLE OF employees.first_name%TYPE INDEX BY PLS_INTEGER;
   TYPE last_typ IS TABLE OF employees.first_name%TYPE INDEX BY PLS_INTEGER;
   first_names first_typ;
   last_names last_typ;
   CURSOR c1 IS SELECT first_name, last_name FROM employees;
   TYPE name_typ IS TABLE OF c1%ROWTYPE INDEX BY PLS_INTEGER;
   all_names name_typ;
   TYPE emp_typ IS TABLE OF employees%ROWTYPE INDEX BY PLS_INTEGER;
   all_employees emp_typ;
BEGIN
-- Query multiple columns from multiple rows, and store them in a collection
-- of records.
   SELECT first_name, last_name BULK COLLECT INTO all_names FROM EMPLOYEES;

-- Query multiple columns from multiple rows, and store them in separate
-- collections. (Generally less useful than a single collection of records.)
   SELECT first_name, last_name
      BULK COLLECT INTO first_names, last_names
      FROM EMPLOYEES;

-- Query an entire (small!) table and store the rows
-- in a collection of records. Now you can manipulate the data
-- in-memory without any more I/O.
   SELECT * BULK COLLECT INTO all_employees FROM employees;
END;
/