select count(*) into total from t1 where c1 in '||p1||';
----------------------------------------------------------
select f1('('''a'',''f'',''e'')') from dual;

解决方案 »

  1.   

    select count(*) into total from t1 where c1 in '||p1||'通不过,在in后面的参数需要()
      

  2.   

    SQL> select * from aa;ID FID
    -- ---
    1  0
    2  1
    3  1
    4  2
    5  3
    6  4
    6  57 rows selectedcreate or replace function f1(p1 in varchar2) 
    return number
    is
    total number;
    str varchar2(50);
    begin
    str:='select count(1) from aa where id in ('||p1||')';
    execute immediate str into total;
    return total;
    exception 
    when no_data_found then
     return 0;
    end;
    /SQL> select f1('''0'',''1'',''2''') from dual;F1('''0'',''1'',''2''')
    -----------------------
                          2你的函数修改如下:
    create or replace function f1(p1 in varchar2) 
    return number
    is
    total number;
    str varchar2(50);
    begin
    str:='select count(*) from t1 where c1 in ('||p1||')';
    execute immediate str into total;
    return total;
    exception 
    when no_data_found then
     return 0;
    end;
      

  3.   

    问题的关键是我的f1中间的参数传给该function后,它作为一个字符串进行处理了。我在存出过程中处理也不行。
      

  4.   

    to beckhambobo(beckham) :
    有可以不使用动态sql语句的方法吗?我实际用的的存储过程非常复杂,如果都写成动态的,太复杂了。实在不行,先将参数写进一个临时表中,再通过in (select c1 from temptable),
    不过不知道是否有其它的方法?
      

  5.   

    重新写一个函数ai来分析你的参数:p1 in varchar2,返回TABLE型变量或数组变量。然后在你的:select count(*) into total from t1 where c1 in (p1);调用。
     比如你的分析函数名为:test,则可以这样:
      select count(*) into total from t1 where c1 in (test(p1));或
      select count(*) into total from t1 where c1 in (select test(p1) from dual);
      

  6.   

    to  qiuyang_wang(小数点):
    我试过了,如果 in(test(p1))中test(p1)的返回值是varray,提示类型不匹配错误,不知能不能提供代码,我现在很急,十分感谢!
      

  7.   

    帮你写了一个测试函数,但还是不行,但只认了一整个字符串,确实有点难以实现。
    create or replace function name_f(p_id in varchar2)
    return varchar2
    as
    str varchar2(20);
    num number;
    n number:=0;
    begin
    num:=instr(p_id,',');
    loop
    if num>0 then
    n:=n+1;
    str:=str||substr(p_id,1,num-1)||',';
    num:=instr(p_id,',',1,n);
    else
    str:=p_id;
    exit;
    end if;
    end loop;
    if instr(str,',',-1,1)=lengthb(str) then
    str:=substr(str,1,lengthb(str)-1);
    end if;
    return str;
    end;
    /在这里它根本就认为一个字串,怎样也分不开。
    SQL> select count(1) from aa where id in (name_f('0,1,2'));  COUNT(1)
    ----------
             0
    在这里,单独一个值就可以
    SQL> select count(1) from aa where id in (name_f('2'));  COUNT(1)
    ----------
             1从中有一个想法,若然能返回个值,而且返回个数动态变的,那有可能实现你功能。
      

  8.   

    beckhambobo(beckham) 写的是对的。使用动态SQL。另外,不要试图捕获select count()的no_data_found异常,捕获不到的。永远捕获不到。
      

  9.   

    算了,我还是改成动态sql语句了,累死我了,谢谢大家!