1. 程式人生 > >儲存過程中動態拼接sql,拼接單引號問題

儲存過程中動態拼接sql,拼接單引號問題

create or replace procedure mergerOrder(
      v_ids in varchar2,
      v_productCode in varchar2,
      v_productCateCode in varchar2) is
      type cur_type is ref cursor;
      cur_policy cur_type;

      rec_policy ecc_pln.order_detail2%rowtype;
      sqlStr varchar2(500);

begin
       sqlStr := 'select id ,order_number from ecc_pln.order_detail2 d where 1=1';
         if v_ids is not null then
           sqlStr := sqlStr || ' and d.id in( SELECT COLUMN_VALUE FROM TABLE(jomoo_oms.SPLIT('''||

v_ids||''')))';
         end if;
         if v_productCode is not null then
           sqlStr := sqlStr || ' and d.material_code='''|| v_productCode||'''';
         end if;
         if v_productCateCode is not null then
           sqlStr := sqlStr || ' and d.product_ca_code=''' || v_productCateCode||''''
;

         end if;
       Dbms_Output.put_line('sqlStr: '||sqlStr);

     open cur_policy for sqlStr;
         loop
          fetch cur_policy into rec_policy.id, rec_policy.order_number;
             exit when cur_policy%notfound;

         Dbms_Output.put_line('id:'||rec_policy.id|| ' order_number:'||rec_policy.order_number);

        end loop;
     close cur_policy;
end mergerOrder;