I cannot create this cursor dynamically, I only have to modify the name of the table in the statement. But it returns me error.
What am I doing wrong or what am I missing to create the dynamic cursor?
The dynamic statement is in the Lv_SQL variable and I call the cursor C_DATOS but it does not recognize it.
PROCEDURE PROC_CAB_DET(Pv_corte VARCHAR2, Pv_MsjError IN OUT VARCHAR2) IS
Lv_Table VARCHAR2(100);
Lv_SQL VARCHAR2(5000);
C_DATOS SYS_REFCURSOR;
BEGIN
Lv_Table := NULL;
IF (Pv_corte IN ('02', '03')) THEN
Lv_Table := 'TABLE_TMP_MOV';
ELSIF (Pv_corte IN ('14', '15')) THEN
Lv_Table := 'TABLE_TMP_FIX';
ELSE
Lv_Table := 'TABLE_TMP_CMF';
END IF;
Lv_SQL := 'SELECT cuenta, campo_2 RUBRO
FROM ' || Lv_Table || '
WHERE codigo = 1
AND CAMPO_3 != "000"
AND (campo_2 NOT IN (SELECT RUBRO FROM GSI_QC_RUBROS_CABECERA)
AND upper(campo_2) NOT LIKE "NAN%")
MINUS
SELECT cuenta, campo_2 RUBRO
FROM ' || Lv_Table || '
WHERE codigo=4
AND campo_2 != "ICE (12%)"';
OPEN C_DATOS FOR Lv_SQL;
FOR I IN C_DATOS LOOP
INSERT INTO GSI_QC_CBS_CASOS_ERROR(CUENTA, ID_ESCENARIO, DATO_TMP_1)
VALUES(I.CUENTA, 'IdEscenario', 'DATA');
END LOOP;
COMMIT;
CLOSE C_DATOS;
EXCEPTION
WHEN OTHERS THEN
Pv_MsjError := SQLERRM;
END PROC_CAB_DET;
PLS-00221: C_DATOS is not a procedure or is undefined
You can't enclose strings into double quotes; have to be single ones. To make it simpler, use the q-quoting mechanism. Also, you wrongly looped through refcursor.
I created dummy tables to make that procedure compile; I don't know whether code does what you planned.
Let's run it: