首页 > 解决方案 > 根据传递的参数列表循环sql结果并将结果存储在oracle中的表中

问题描述

我有类似类型的参数列表,其值为 TRS:5,IRS:10(值用冒号分隔,第一个值是类型,第二个值是要从表中获取的记录)

我想根据传递的参数数量循环sql并将结果存储在表中,

所以,基于下面sql的参数应该循环2次并将结果存储在表中

typology1=TRS,IRS
rownumber1=5,10
for loop in ( select contract,typolgy from table ABC 
              where typology =: typology1 
                and rownum=:rownumber1)
<store value in a table>
end

ABC 表数据:

Contract Typolgy
123      TRS
123      TRS
123      TRS
123      TRS
123      TRS
123      TRS
1231     TRS
1232     TRS
1234     TRS
1235     TRS
1236     TRS
1237     TRS
1237     IRS

标签: sqloraclestored-proceduresoracle-sqldeveloper

解决方案


根据您的描述,您不需要循环;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>

推荐阅读