【问题标题】:loop sql result based on the parameter list passed and store result in table in oracle根据传递的参数列表循环sql结果并将结果存储在oracle中的表中
【发布时间】:2020-06-06 11:51:22
【问题描述】:

我有参数列表,例如类型学,其值为 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

【问题讨论】:

    标签: sql oracle stored-procedures oracle-sqldeveloper


    【解决方案1】:

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

    【讨论】:

    • 谢谢...但参数将类似于下面的 exec p_test('IRS:5,TRS:4,REPO:6')。所以根据类型学和 rownum ,它应该获取数据并存储在表中。
    • for l_tables in ( SELECT level,trim(regexp_substr(v_typology_reccount, '[^,]+', 1, LEVEL)) job_id_row FROM dual CONNECT BY regexp_substr(v_typology_reccount , '[^,]+' , 1, LEVEL) IS NOT NULL ) loop v_typology := REGEXP_SUBSTR (l_tables.job_id_row, '[^:]+', 1, 1); v_reccount := to_number(REGEXP_SUBSTR (l_tables.job_id_row, '[^:]+', 1, 2));
    • 循环 v_typology := REGEXP_SUBSTR (l_tables.job_id_row, '[^:]+', 1, 1); v_reccount := to_number(REGEXP_SUBSTR (l_tables.job_id_row, '[^:]+', 1, 2)); v_sql_stmt :='插入 TRN_TYPOLOGY_REP(first_name,job_id) select first_name,job_id from EMPLOYEES where upper(job_id) = ''' ||上(v_typology)|| ''' 和 rownum
    • 不客气。如果输入参数看起来像你说的,那么你必须把它分成更小的部分;其余程序相同。我编辑了答案并添加了更多代码。请看一下。
    猜你喜欢
    • 1970-01-01
    • 2020-04-17
    • 2021-12-16
    • 2022-07-01
    • 2015-08-24
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    相关资源
    最近更新 更多