Ver Mensaje Individual
  #1 (permalink)  
Antiguo 12/09/2018, 16:50
tsuno123
 
Fecha de Ingreso: marzo-2015
Mensajes: 7
Antigüedad: 9 años, 1 mes
Puntos: 0
Problema con secuenciamiento de Oracle

Hola

Tengo un problema con el secuenciamiento en oracle, lo que sucede es que cuando voy creando registros a la tabla que contiene el secuenciamiento el id se genera de manera desordenada.

[URL="https://ibb.co/iCusX9"]https://ibb.co/iCusX9[/URL]

Código de la secuencia

[URL="https://ibb.co/cLtgaU"]https://ibb.co/cLtgaU[/URL]

Código SQL:
Ver original
  1. CREATE SEQUENCE JSENACE_GEADM.AGENDA_ID_SEQ INCREMENT BY 1 MAXVALUE 999999999999999999999999 MINVALUE 1 CACHE 20

Tabla donde se encuentra la secuencia

[URL="https://ibb.co/cLenFU"]https://ibb.co/cLenFU[/URL]

Código SQL:
Ver original
  1. TRIGGER "JSENACE_GEADM".Agenda_Id_TRG BEFORE INSERT ON Agenda
  2. FOR EACH ROW
  3. DECLARE
  4. v_newVal NUMBER(12) := 0;
  5. v_incval NUMBER(12) := 0;
  6. BEGIN
  7.   IF INSERTING AND :NEW.Id IS NULL THEN
  8.     SELECT  Agenda_Id_SEQ.NEXTVAL INTO v_newVal FROM DUAL;
  9.     -- If this is the first time this table have been inserted into (sequence == 1)
  10.     IF v_newVal = 1 THEN
  11.       --get the max indentity value from the table
  12.       SELECT NVL(MAX(Id),0) INTO v_newVal FROM Agenda;
  13.       v_newVal := v_newVal + 1;
  14.       --set the sequence to that value
  15.       LOOP
  16.            EXIT WHEN v_incval>=v_newVal;
  17.            SELECT Agenda_Id_SEQ.NEXTVAL INTO v_incval FROM dual;
  18.       END LOOP;
  19.     END IF;
  20.   -- save this to emulate @@identity
  21.   utils.identity_value := v_newVal;
  22.    -- assign the value from the sequence to emulate the identity column
  23.    :NEW.Id := v_newVal;
  24.   END IF;
  25. END;

Última edición por tsuno123; 12/09/2018 a las 16:59