I was writting an anonymous PLSQL block to re-synchronize Sequences, sometimes Sequences get out of wack with their target tables. Fine, so I want to simply increment the sequence. But, neither Native Dynamic Sql nor DBMS_SQL approaches is working for me. Are Sequences excluded from being manipulated these two ways? No hard coding, I want generic code that works in any schema.
Here is my example of using DBMS_SQL, the loop is working for SELECT ....NEXTVAL FROM DUAL, but no incrementing of the Sequence ever happens. What am I doing wrong?
--
-- Found and defined elsewhere in the file are these definitions. Just a array of Sequences with
-- the table name and the max PK value generated from the Sequences. Then a
-- record type that matches the array.
--
TYPE gTabRec_Typ IS RECORD
(
Sequence_Name User_Sequences.Sequence_Name%TYPE,
Last_Number User_Sequences.Last_Number%TYPE,
Table_Name User_Tables.Table_Name%TYPE,
MaxPk INTEGER
);
TYPE gLuSequencesType_Nt IS TABLE OF gTabRec_Typ INDEX BY BINARY_INTEGER;
gSeqRec gTabRec_Typ;
gLuSequences gLuSequencesType_Nt;
. . .
-- Here starts an ebedded Procedure that completely ignores my incrementing efforts for the called Sequences.
-- And yes, I have checked to see that the Sequences are being properly called up
-- and values in the array are indeed correct.
-- Loops are working as they should be.
PROCEDURE FixSequences_Prc IS
vCursorNr INTEGER DEFAULT DBMS_SQL.OPEN_CURSOR;
vSqlStmt VARCHAR2(200);
vIndx INTEGER := gLuSequences.FIRST;
vCnt1 INTEGER := 0;
vCnt2 INTEGER := 0;
vProsRows INTEGER := 0;
BEGIN
LOOP
EXIT WHEN vIndx IS NULL;
gSeqRec := gLuSequences(vIndx);
IF gSeqRec.Last_Number <= gSeqRec.MaxPk THEN
vCnt1 := gSeqRec.Last_Number;
vCnt2 := gSeqRec.MaxPk + 1;
vSqlStmt := 'SELECT '||gSeqRec.Sequence_Name||'.NEXTVAL FROM DUAL';
DBMS_SQL.Parse (vCursorNr, vSqlStmt, DBMS_SQL.NATIVE);
WHILE vCnt1 <= vCnt2 LOOP
vProsRows := DBMS_SQL.EXECUTE (vCursorNr);
vCnt1 := vCnt1 + 1;
END LOOP;
DBMS_SQL.CLOSE_CURSOR (vCursorNr);
END IF;
vIndx := gLuSequences.NEXT (vIndx);
END LOOP;
END FixSequences_Prc;
. . .
Here is my example of using DBMS_SQL, the loop is working for SELECT ....NEXTVAL FROM DUAL, but no incrementing of the Sequence ever happens. What am I doing wrong?
--
-- Found and defined elsewhere in the file are these definitions. Just a array of Sequences with
-- the table name and the max PK value generated from the Sequences. Then a
-- record type that matches the array.
--
TYPE gTabRec_Typ IS RECORD
(
Sequence_Name User_Sequences.Sequence_Name%TYPE,
Last_Number User_Sequences.Last_Number%TYPE,
Table_Name User_Tables.Table_Name%TYPE,
MaxPk INTEGER
);
TYPE gLuSequencesType_Nt IS TABLE OF gTabRec_Typ INDEX BY BINARY_INTEGER;
gSeqRec gTabRec_Typ;
gLuSequences gLuSequencesType_Nt;
. . .
-- Here starts an ebedded Procedure that completely ignores my incrementing efforts for the called Sequences.
-- And yes, I have checked to see that the Sequences are being properly called up
-- and values in the array are indeed correct.
-- Loops are working as they should be.
PROCEDURE FixSequences_Prc IS
vCursorNr INTEGER DEFAULT DBMS_SQL.OPEN_CURSOR;
vSqlStmt VARCHAR2(200);
vIndx INTEGER := gLuSequences.FIRST;
vCnt1 INTEGER := 0;
vCnt2 INTEGER := 0;
vProsRows INTEGER := 0;
BEGIN
LOOP
EXIT WHEN vIndx IS NULL;
gSeqRec := gLuSequences(vIndx);
IF gSeqRec.Last_Number <= gSeqRec.MaxPk THEN
vCnt1 := gSeqRec.Last_Number;
vCnt2 := gSeqRec.MaxPk + 1;
vSqlStmt := 'SELECT '||gSeqRec.Sequence_Name||'.NEXTVAL FROM DUAL';
DBMS_SQL.Parse (vCursorNr, vSqlStmt, DBMS_SQL.NATIVE);
WHILE vCnt1 <= vCnt2 LOOP
vProsRows := DBMS_SQL.EXECUTE (vCursorNr);
vCnt1 := vCnt1 + 1;
END LOOP;
DBMS_SQL.CLOSE_CURSOR (vCursorNr);
END IF;
vIndx := gLuSequences.NEXT (vIndx);
END LOOP;
END FixSequences_Prc;
. . .