删除单条delete from table where id in(1);这样可以,但是我传多个参数的时候由于是字符串的了('12,13,14')所以不能够完成删除操作,怎么写个存储过程完成这种操作delete from table where id in('12,13,14')呢 谢谢!!

解决方案 »

  1.   

    --摘自asktom
    --str2tbl, 将逗号分隔的字符串转成表
    --考虑:写成piple row函数,可优化性能create or replace type str2tblType as table of varchar2(30);
    /create or replace
    function str2tbl( p_str in varchar2, p_delim in varchar2 default ',' )
    return str2tblType
    as
        l_str      long default p_str || p_delim;
        l_n        number;
        l_data     str2tblType := str2tbltype();
    begin
        dbms_application_info.set_client_info( userenv('client_info')+1 );
        loop
            l_n := instr( l_str, p_delim );
            exit when (nvl(l_n,0) = 0);
                    l_data.extend;
                    l_data(l_data.count) := ( ltrim(rtrim(substr(l_str,1,l_n-1))) );
            l_str := substr( l_str, l_n+1 );
        end loop;
        return l_data;
    end;
    /
      

  2.   

    在存储过程中使用动态SQL
    execute immediate 'delete from table where id in('|| i_param_list || ')';
      

  3.   

    delete from t where id in (select column_value from table(cast(str2tbl('1,2,3,4') as str2tblType));
      

  4.   

    这个:
    delete from t where id in (select column_value from table(cast(str2tbl('1,2,3,4') as str2tblType)));
      

  5.   

    用tom的,可以用到绑定变量,不过10g完全可以用正则表达式一条sql搞定。
    动态sql缺点明显,数量不定,使用不了绑定变量,而且in中还有列表数小于1000的限制
      

  6.   

    id是数值型的时候不要加引号。
    delete from table where id in (12,13,14);
    id是字符型的时候,这样加引号。
    delete from table where id in ('12','13','14');
      

  7.   


    execute Immediate 'delete From table 
    Where Id In (''regexp_replace(''12,13,14'','','','''','''')'') '
      

  8.   


    在存储过程的参数中传入字符串(你要删除数据的ID)
    例如:v_del_id := '1,2,3,4';在存储过程中,动态调用SQL
    例如:
    execute immediate 'delete from tablename where id in('|| v_del_id || ')';
      

  9.   


    delete from table where ','||to_char(id)||',' in(',12,13,14,')
      

  10.   

    delete from table where id in ('12','13','14');
    这样就可以了,是你sql的问题
      

  11.   

    delete from table where id in replace('12,13,14',',',''',''');