Oracle Parameters with IN statement?

前端 未结 5 1427
忘了有多久
忘了有多久 2020-11-30 08:14

Got a c#.net app which I need to modify. The query at the moment effectively does this:

select * from contract where contractnum = :ContractNum
5条回答
  •  臣服心动
    2020-11-30 08:53

    you could use a pipelined function to transform a string into a table which could be used with the IN operator. For example (tested with 10gR2):

    SQL> select * from table(demo_pkg.string_to_tab('i,j,k'));
    
    COLUMN_VALUE
    -----------------
    i
    j
    k
    

    with the following package:

    SQL> CREATE OR REPLACE PACKAGE demo_pkg IS
      2     TYPE varchar_tab IS TABLE OF VARCHAR2(4000);
      3     FUNCTION string_to_tab(p_string VARCHAR2,
      4                            p_delimiter VARCHAR2 DEFAULT ',')
      5        RETURN varchar_tab PIPELINED;
      6  END demo_pkg;
      7  /
    
    Package created
    SQL> CREATE OR REPLACE PACKAGE BODY demo_pkg IS
      2     FUNCTION string_to_tab(p_string VARCHAR2,
      3                            p_delimiter VARCHAR2 DEFAULT ',')
      4        RETURN varchar_tab PIPELINED IS
      5        l_string          VARCHAR2(4000) := p_string;
      6        l_first_delimiter NUMBER := instr(p_string, p_delimiter);
      7     BEGIN
      8        LOOP
      9           IF nvl(l_first_delimiter,0) = 0 THEN
     10              PIPE ROW(l_string);
     11              RETURN;
     12           END IF;
     13           PIPE ROW(substr(l_string, 1, l_first_delimiter - 1));
     14           l_string          := substr(l_string, l_first_delimiter + 1);
     15           l_first_delimiter := instr(l_string, p_delimiter);
     16        END LOOP;
     17     END;
     18  END demo_pkg;
     19  /
    
    Package body created
    

    Your query would look like this:

    select * 
      from contract 
     where contractnum in (select column_value
                             from table(demo_pkg.string_to_tab(:ContractNum)))
    

提交回复
热议问题