Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

find column that a sequence is being used.

oratechsDec 19 2021 — edited Dec 19 2021

Hello Gurus,
we have an issue in our organization, while performing some maintenance our DBA messed up sequences and now we need to find all the sequences and the table primary key column that these are being used. and see I sequence need to be incremented or not, I got some success with the following script where primary key column matches exactly that of sequence name, but is there any script or any way I can find primary key columns and sequences that do not match. my last option would be to search in the application code.
SELECT ac.owner, ac.TABLE_NAME,acc.COLUMN_NAME, ds.SEQUENCE_NAME , ds.LAST_NUMBER,acc.POSITION,ac.CONSTRAINT_NAME
FROM All_Cons_Columns acc , All_Constraints ac, Dba_Sequences ds
WHERE ac.CONSTRAINT_NAME = acc.CONSTRAINT_NAME
AND ac.CONSTRAINT_TYPE ='P' --AND acc.POSITION =1
AND ds.SEQUENCE_OWNER = ac.OWNER
AND ds.SEQUENCE_NAME = acc.COLUMN_NAME
AND ac.OWNER = acc.OWNER
ORDER BY ac.TABLE_NAME, acc.position
;

Comments
Post Details
Added on Dec 19 2021
7 comments
6,332 views