Question

Before asking this question, I was checking if there were any similar questions, but I did not find what I'm looking for. Here we go!.

The problem is that I have a cursor that is crossed, but believe declare a variable that while I run the courses I perform a validation, I would like that when the validation is TRUE, I can jump to the next "row" of the cursor, skipping this "record" so that the following cursor stages do not run.

I know that in the CURSOR I could generate a "sub-query" doing the validation directly, but I would like to be able to learn if it is possible to perform this "jump" of the cursor.

Here is a bit of my code.

DECLARE queryCriterionHasArticle CURSOR FOR 
SELECT 
  CCLA.CLEG_ID, 
  CCLA.ALEG_ID, 
  CCLA.CCL_ID, 
  CCLA.CCLA_ID, 
  CCLAC.CRCA_ID 
FROM 
  SER_LEGAL.CONFIGURACION_has_CUERPO_LEGAL_has_ARTICULO_CUMPLIMIENTO AS CCLAC 
  JOIN SER_LEGAL.CONFIGURACION_has_CUERPO_LEGAL_has_ARTICULO AS CCLA ON CCLA.CCLA_ID = CCLAC.CCLA_ID 
  AND CCLA.ENT_ID = companyId 
  AND CCLA.CCLA_ESTADO = 1 
  AND CCLA.ESA_ID = 1 
  AND CCLA.CCLA_TIPO_CLASIFICACION = 1 
WHERE 
  CCLAC.CRC_ID = criterionId;
DECLARE CONTINUE HANDLER FOR NOT FOUND 
SET 
  noMoreRowsOne = TRUE;
-- ####################################################################################    
-- Se comienza a recorrer un cursor que tiene los articulos asociados al criterio
-- ####################################################################################    
OPEN queryCriterionHasArticle;
queryCriterionHasArticle_LOOP : LOOP FETCH queryCriterionHasArticle INTO legalBodyId, 
legalArticleId, 
legalBodyConfigId, 
legalArticleConfigId, 
criterionConfigId;
IF noMoreRowsOne THEN CLOSE queryCriterionHasArticle;
LEAVE queryCriterionHasArticle_LOOP;
END IF;
-- ####################################################################################
-- 11/06/2019
-- Se procede a agregar una validacion donde no se conside aquellos articulos que tengan
-- una evaluacion en SER_LEGAL.EVALUACION_has_ARTICULO WHERE EHA_TIPO = 1 AND EHA_ORIGEN = 2
-- debido a que estos articulos han sido evaludos con la integracion SISQUIM/ALMACENAMIENTO
-- estos articulos no se pueden modificar desde pre-evaluacion
-- ####################################################################################
SET 
  validationIntegrationSisquim = (
    SELECT 
      IF(
        COUNT(0) = 1, 
        TRUE, 
        FALSE
      ) 
    FROM 
      SER_LEGAL.EVALUACION_has_ARTICULO 
    WHERE 
      CCL_ID = legalBodyConfigId 
      AND CCLA_ID = legalArticleConfigId 
      AND EHA_TIPO = 1 
      AND EHA_ORIGEN = 2
  );
IF (
  validationIntegrationSisquim IS TRUE
) THEN -- ¿¿¿¿¿LEAVE queryCriterionHasArticle_LOOP;?????
END IF;
END LOOP;

I appreciate your help and guides.

Was it helpful?

Solution

Cursors can usually be turned into queries. Programming SQL with cursors in avoiding the power of SQL.

But first, a lesson on ordering. A "table" is unordered. Doing a SELECT gives you no guarantee of what the rows will be delivered in -- unless you have an ORDER BY clause.

Is this what you are trying to do? "Fetch some rows from a combination of tables, but stop when some condition is met."?

If, instead, you need to "Fetch all the rows from a combination of tables, except the rows that meet some condition", then a single query like this will suffice:

SELECT ...
    FROM a JOIN b ...
    WHERE NOT EXISTS ( SELECT 1 FROM SER_LEGAL.EVALUACION_has_ARTICULO WHERE ... )

No cursor needed.

If you really need to stop at the "first" occurrence of such, then provide the ORDER BY so we can discuss further.

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top