Oracle存储过程,拆分VARCHAR和处理案例

时间:2018-10-16 16:03:53

标签: oracle stored-procedures split

我这样做有点麻烦,希望有人能帮忙。

我有这个存储过程:

CREATE OR REPLACE PROCEDURE FILTER_COPY(
        vOrigen IN VARCHAR2,
        sListadoClientes__ OUT SYS_REFCURSOR
       )
        IS
        vParam VARCHAR2(10);
        vQuery_final VARCHAR2(2500);
BEGIN
    DECLARE BEGIN
        FOR i IN
     (SELECT regexp_substr(vOrigen ,'[^,]+', 1, level) AS params FROM dual
             CONNECT BY regexp_substr(vOrigen, '[^,]+', 1, level) IS NOT NULL)
         LOOP
         vParam := i.params;
         END LOOP;
    CASE (vOrigen)
        WHEN 'CALLCENTER' THEN vQuery_final:= 'SELECT IDCALL as indice, NOMBRE as nombre FROM CALL ORDER BY 1';

        WHEN 'PAIS' THEN vQuery_final:= 'SELECT CODE as indice, NAME as nombre FROM PAIS WHERE CODE IN ('''|| 'CAN' ||''',''' || 'USA' ||''') ORDER BY 1';

        WHEN 'HOTEL' THEN vQuery_final:= 'SELECT HOTEL_ID as indice,NOMBRE as nombre FROM HOTEL WHERE TIPO = '' || ROYAL || '' ORDER BY 1';

        WHEN 'NOTA' THEN vQuery_final:= 'SELECT IDNOTA as indice, TIPONOTA as nombre FROM NOTAS ORDER BY 1';

        WHEN 'DEPARTAMENTO' THEN vQuery_final:= 'SELECT IDDEPTO as indice, DEPTO as nombre FROM DEPATRAMENTO WHERE MESSAGE = 1 ORDER BY 1';

    END CASE;
    -- query_final:='SELECT DUMMY  FROM DUAL';
    OPEN sListadoClientes__ FOR vQuery_final;
     --select vParam as CLIENT from dual;
  END;
END;

我的问题是,当IN参数“ vOrigen”只是一种情况,或者如果我需要拆分以完成两个参数时,我不知道如何处理,此存储过程的起源在“ HOTEL”中这种情况是因为我需要获取ORIGIN才能完成select语句。

感谢您的时间。

2 个答案:

答案 0 :(得分:0)

当vOrigen为“ HOTEL,CUN”并且您要在选择情况下使用“ HOTEL”和“ CUN”时,您可以尝试如下操作:

declare
    vQuery_final VARCHAR2(2500);
    vOrigen varchar2(2000);
    type param_table is table of varchar2(100) index by PLS_INTEGER;
    t_param param_table;
BEGIN
    vOrigen := 'HOTEL, CUN';

    FOR i IN (SELECT regexp_substr(vOrigen ,'[^,]+', 1, level) AS params, level as nr FROM dual
         CONNECT BY regexp_substr(vOrigen, '[^,]+', 1, level) IS NOT NULL)
     LOOP
        t_param(i.nr) := trim(i.params);--delete spaces
        dbms_output.put_line(i.nr||' param='||t_param(i.nr));
     END LOOP;

    CASE (t_param(1))
        WHEN 'CALLCENTER' THEN vQuery_final:= 'SELECT IDCALL as indice, NOMBRE as nombre FROM CALLCENTER ORDER BY 1';

        WHEN 'PAIS' THEN vQuery_final:= 'SELECT CODE as indice, NAME as nombre FROM PAIS WHERE CODE IN ('''|| 'CAN' ||''',''' || 'USA' ||''') ORDER BY 1';

        WHEN 'HOTEL' THEN vQuery_final:= 'SELECT HOTEL_ID as indice,NOMBRE as nombre FROM HOTEL WHERE TIPO = ''' || t_param(2) || ''' ORDER BY 1';

        WHEN 'NOTA' THEN vQuery_final:= 'SELECT IDNOTA as indice, TNOTA as nombre FROM NOTAS ORDER BY 1';

        WHEN 'DEPARTAMENTO' THEN vQuery_final:= 'SELECT IDDEPTO as indice, DEPTO as nombre FROM DEPARTAMENTO WHERE MESSAGE = 1 ORDER BY 1';

        ELSE vQuery_final:= 'select 1 from dual';
    END CASE;

    dbms_output.put_line('vQuery_final='||vQuery_final);
END;

答案 1 :(得分:0)

另一种拆分参数组合“ NOTA”或“ HOTEL,CUN”的方法

CREATE OR REPLACE PROCEDURE FILTER_COPY(
        vOrigen IN VARCHAR2,
        sListadoClientes__ OUT SYS_REFCURSOR
       )
        IS
   vParam VARCHAR2(10);
   vQuery_final VARCHAR2(2500);
   v_param1  varchar2(10);
   v_param2  varchar2(10);
BEGIN

SELECT  REGEXP_SUBSTR (vOrigen, '[^,]+', 1, 1) 
INTO v_param1
from dual;

SELECT  REGEXP_SUBSTR (vOrigen, '[^,]+', 1, 2)
INTO v_param2
from dual;

    CASE (v_param1)
        WHEN 'CALLCENTER' THEN vQuery_final:= 'SELECT IDCALLC as indice, NOMBRE as nombre FROM CALLCENTER ORDER BY 1';

        WHEN 'PAIS' THEN vQuery_final:= 'SELECT CODE as indice, NAME as nombre FROM PAIS WHERE CODE IN ('''|| 'CAN' ||''',''' || 'USA' ||''') ORDER BY 1';

        WHEN 'HOTEL' THEN vQuery_final:= 'SELECT HOTEL_ID as indice,NOMBRE as nombre FROM HOTEL WHERE TIPO = ''' || v_param2 || ''' ORDER BY 1';

        WHEN 'NOTA' THEN vQuery_final:= 'SELECT IDTNOTA as indice, TIPONOTA as nombre FROM NOTAS ORDER BY 1';

        WHEN 'DEPARTAMENTO' THEN vQuery_final:= 'SELECT IDDEPTO as indice, DEPTO as nombre FROM DEPARTAMENTO WHERE MESSAGE = 1 ORDER BY 1';

        ELSE vQuery_final:= 'select 1 from dual';
    END CASE;

END;