根据您的描述,您不需要循环; select可以自己做。
示例数据:我修改了contract 的值,以便在测试时更容易区分。
SQL> desc test
Name Null? Type
----------------------------------------- -------- ----------------------------
CONTRACT NUMBER
TYPOLOGY VARCHAR2(10)
SQL> select * From test;
no rows selected
SQL> select * From abc;
CONTRACT TYPOLOGY
---------- ----------
123 TRS
124 TRS
125 TRS
126 TRS
127 TRS
128 TRS
1231 TRS
1232 TRS
1234 TRS
1235 TRS
1236 TRS
1237 TRS
1237 IRS
13 rows selected.
程序:我认为您想做的事情没有多大意义,但是 - 我想这只是更复杂的事情的一个例子。无论如何:regexp_substr 用于提取输入参数的第一(拓扑)和第二(计数器)部分。
SQL> create or replace procedure p_test (par_typ in varchar2)
2 is
3 begin
4 insert into test (contract, typology)
5 select a.contract, a.typology
6 from abc a
7 where a.typology = regexp_substr(par_typ, '^\w+')
8 and rownum <= to_number(regexp_substr(par_typ, '\w+$'));
9 end;
10 /
Procedure created.
测试:
SQL> exec p_test('TRS:3');
PL/SQL procedure successfully completed.
SQL> select * From test;
CONTRACT TYPOLOGY
---------- ----------
123 TRS
124 TRS
125 TRS
SQL> exec p_test('IRS:5');
PL/SQL procedure successfully completed.
SQL> select * From test;
CONTRACT TYPOLOGY
---------- ----------
123 TRS
124 TRS
125 TRS
1237 IRS
SQL>
如果,正如您所评论的,输入参数由几个部分组成,然后将其拆分为更小的部分(请参阅FOR LOOP 查询),然后像我已经展示的那样使用该片段。
SQL> create or replace procedure p_test (par_typ in varchar2)
2 is
3 begin
4 for cur_r in
5 (select regexp_substr(par_typ, '[^,]+', 1, level) l_typ
6 from dual
7 connect by level <= regexp_count(par_typ, ',') + 1
8 )
9 loop
10 insert into test (contract, typology)
11 select a.contract, a.typology
12 from abc a
13 where a.typology = regexp_substr(cur_r.l_typ, '^\w+')
14 and rownum <= to_number(regexp_substr(cur_r.l_typ, '\w+$'));
15 end loop;
16 end;
17 /
Procedure created.
SQL> exec p_test('IRS:5,TRS:4,REPO:6');
PL/SQL procedure successfully completed.
SQL> select * From test;
CONTRACT TYPOLOGY
---------- ----------
1237 IRS
123 TRS
124 TRS
125 TRS
126 TRS
SQL>