sql >> Base de Datos >  >> RDS >> Oracle

bucle for dentro de un oráculo de cursor

Siempre se abre un cursor en una instrucción SELECT. No hay forma, que yo sepa, de abrir un cursor en un FOR círculo.

Me parece que realmente desea crear una declaración SELECT dinámicamente. Sugiero algo como lo siguiente:

PROCEDURE p_get_xxx(p_id IN VARCHAR2, p_cur_result OUT SYSREFCURSOR)
AS
  l_array schema_name.t_array;
  strSelect_statement  VARCHAR2(4000);
BEGIN
  l_array := split_string(p_id);

  -- Set up the basics of the SELECT statement

  strSelect_statement := 'SELECT * FROM SOME_TABLE WHERE ID IN (';

  FOR i IN l_array.FIRST..l_array.LAST LOOP
    strSelect_statement := strSelect_statement ||
                             '''' || l_array(i) || ''',';
  END LOOP;

  -- Get rid of the unwanted trailing comma

  strSelect_statement := SUBSTR(strSelect_statement, 1,
                                  LENGTH(strSelect_statement)-1);

  -- Add a right parentheses to close the IN list

  strSelect_statement := strSelect_statement || ')';

  -- Open the cursor

  OPEN p_cur_result FOR strSelect_statement;
END p_get_xxx;

Mucha suerte.