Vectores en PL

Necesito alguna alternativa para manejar arreglos o vectores de mas de una dimensión, dentro de un procedimiento Oracle

1 respuesta

Respuesta
1
Esto te irá de perlas, fíjate en el EJEMPLO.
Create or replace package my_array
AS
PROCEDURE DIM( P_DIM1 IN NUMBER DEFAULT NULL,
P_DIM2 IN NUMBER DEFAULT NULL,
P_DIM3 IN NUMBER DEFAULT NULL,
P_DIM4 IN NUMBER DEFAULT NULL );
PRAGMA RESTRICT_REFERENCES( DIM, WNDS);
FUNCTION GETVAL( P_IDX1 IN NUMBER DEFAULT NULL,
P_IDX2 IN NUMBER DEFAULT NULL,
P_IDX3 IN NUMBER DEFAULT NULL,
P_IDX4 IN NUMBER DEFAULT NULL ) RETURN NUMBER;
PRAGMA RESTRICT_REFERENCES( GETVAL, WNDS, RNDS );
PROCEDURE SETVAL( P_VALUE IN NUMBER,
P_IDX1 IN NUMBER DEFAULT NULL,
P_IDX2 IN NUMBER DEFAULT NULL,
P_IDX3 IN NUMBER DEFAULT NULL,
P_IDX4 IN NUMBER DEFAULT NULL );
PRAGMA RESTRICT_REFERENCES( SETVAL, WNDS, RNDS );
PROCEDURE REMOVE;
PRAGMA RESTRICT_REFERENCES( REMOVE, WNDS );
PRAGMA RESTRICT_REFERENCES( MY_ARRAY, WNDS, RNDS, WNPS, RNPS );
PROCEDURE EJEMPLO;
END MY_ARRAY;
/
CREATE OR REPLACE PACKAGE BODY MY_ARRAY
AS
TYPE NARRAY IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
G_DIM1 NUMBER DEFAULT 0;
G_DIM2 NUMBER DEFAULT 0;
G_DIM3 NUMBER DEFAULT 0;
G_DIM4 NUMBER DEFAULT 0;
G_DIM2_3 NUMBER;
G_DIM3_4 NUMBER;
G_DIM2_3_4 NUMBER;
G_NDIM NUMBER DEFAULT 0;
G_ARRAY NARRAY;
PROCEDURE DIM( P_DIM1 IN NUMBER DEFAULT NULL,
P_DIM2 IN NUMBER DEFAULT NULL,
P_DIM3 IN NUMBER DEFAULT NULL,
P_DIM4 IN NUMBER DEFAULT NULL )IS
/******************************************************************************
Nombre: DIM
Tipo: Procedure
Descripcion: Dimensiona el array (1 a 4 dimensiones).
******************************************************************************/
BEGIN
G_DIM1 := NVL(P_DIM1,0);
G_DIM2 := NVL(P_DIM2,0);
G_DIM3 := NVL(P_DIM3,0);
G_DIM4 := NVL(P_DIM4,0);
G_DIM2_3 := G_DIM2 * G_DIM3;
G_DIM3_4 := G_DIM3 * G_DIM4;
G_DIM2_3_4 := G_DIM2 * G_DIM3 * G_DIM4;
SELECT DECODE( P_DIM1, NULL, 0,
DECODE( P_DIM2, NULL, 1,
DECODE( P_DIM3, NULL, 2,
DECODE( P_DIM4, NULL, 3, 4 ) ) ) )
INTO G_NDIM
FROM DUAL;
END;
FUNCTION IDX_OF( P_IDX1 IN NUMBER DEFAULT 0,
P_IDX2 IN NUMBER DEFAULT 0,
P_IDX3 IN NUMBER DEFAULT 0,
P_IDX4 IN NUMBER DEFAULT 0 ) RETURN NUMBER IS
/******************************************************************************
Nombre: IDX_OF
Tipo: Función
Descripcion: Llamada por Getval, retorna el valor indicado por los indices.
******************************************************************************/
L_IDX NUMBER;
BEGIN
IF ( G_NDIM = 1 ) THEN
L_IDX := P_IDX1;
ELSIF ( G_NDIM = 2 ) THEN
L_IDX := P_IDX1 * G_DIM2 +
P_IDX2;
ELSIF ( G_NDIM = 3 ) THEN
L_IDX := P_IDX1 * G_DIM2_3 +
P_IDX2 * G_DIM3 +
P_IDX3;
ELSIF ( G_NDIM = 4 ) THEN
L_IDX := P_IDX1 * G_DIM2_3_4 +
P_IDX2 * G_DIM3_4 +
P_IDX3 * G_DIM4 +
P_IDX4;
END IF;
RETURN L_IDX;
END;
FUNCTION GETVAL( P_IDX1 IN NUMBER DEFAULT 0,
P_IDX2 IN NUMBER DEFAULT 0,
P_IDX3 IN NUMBER DEFAULT 0,
P_IDX4 IN NUMBER DEFAULT 0 ) RETURN NUMBER IS
/******************************************************************************
Nombre: GETVAL
Tipo: Función
Descripcion: Retorna el valor indicado por los indices.
******************************************************************************/
BEGIN
RETURN G_ARRAY( IDX_OF(P_IDX1,P_IDX2,P_IDX3,P_IDX4) );
END;
PROCEDURE SETVAL( P_VALUE IN NUMBER,
P_IDX1 IN NUMBER DEFAULT NULL,
P_IDX2 IN NUMBER DEFAULT NULL,
P_IDX3 IN NUMBER DEFAULT NULL,
P_IDX4 IN NUMBER DEFAULT NULL )IS
/******************************************************************************
Nombre: SETVAL
Tipo: Procedure
Descripcion: Añade un valor en la posición indicada del array.
**********************************************************

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas