将多个输入字符串传递给Oracle查询

时间:2018-02-01 16:24:57

标签: oracle

我传递的输入值是“MI,NOKIA,APPLE'一个程序。必须将此输入值传递给where condition中的查询。

例如:

create or replace procedure abc123(p_name varchar2)
is
v_val number;
begin
 dbms_output.put_line ('p_name: '||p_name );

 Select 1 
 Into v_val
 from MYTABLE
 where Model in p_name;

 dbms_output.put_line ('v_val: '||v_val );
end;

这不起作用。如何将多个输入值作为单个参数传递?

2 个答案:

答案 0 :(得分:0)

IN运算符后使用paranthesis:

Select 1 Into v_val from MYTABLE where Model in (p_name);

答案 1 :(得分:0)

尝试以下代码,

create or replace procedure proc123 (p_param VARCHAR2)
IS
     TYPE cur_typ IS REF CURSOR;
     c cur_typ;
     v_query VARCHAR2(200) := 'SELECT *  '||
                               'FROM (SELECT ''Model1'' model FROM dual '||
                               'union all '||
                               'SELECT ''Model2'' FROM dual '||
                               'union all '||
                               'SELECT ''Model3'' FROM dual)';
      v_model VARCHAR2(20);
BEGIN
     v_query := v_query||' WHERE INSTR('''||p_param||''',model) > 0';
     dbms_output.put_line(v_query);
     OPEN c FOR v_query;
     LOOP
         FETCH c INTO v_model;
         EXIT WHEN c%NOTFOUND;
         dbms_output.put_line(v_model);
     END LOOP;
     CLOSE c;
END;
/

--set your dbms_output on
begin
    proc123('Model1, Model2');
end;
/

如果要在查询中传递参数,则可以使用INSTR函数

 Select 1 
   from MYTABLE 
  where INSTR(p_name, model) > 0;