ORA-02239: There Are Objects Which Reference This Sequence
ORA-02239 occurs in Oracle Database when you attempt to drop a sequence that is still being referenced by one or more dependent objects, such as triggers, views, stored procedures, functions, or packages. Oracle's dependency tracking mechanism blocks the drop operation to protect referential integrity within the schema. This error is commonly encountered during schema migrations, deployment scripts, or database cleanup operations.
Top 3 Causes
1. A Trigger References the Sequence
The most common cause is a BEFORE INSERT trigger that uses SEQUENCE.NEXTVAL to auto-populate a primary key column.
-- Find triggers referencing the sequence
SELECT d.name AS trigger_name, d.type
FROM user_dependencies d
WHERE d.referenced_name = 'SEQ_EMP_ID'
AND d.referenced_type = 'SEQUENCE'
AND d.type = 'TRIGGER';
-- Example of a trigger that causes ORA-02239
CREATE OR REPLACE TRIGGER TRG_EMP_INSERT
BEFORE INSERT ON EMPLOYEES
FOR EACH ROW
BEGIN
SELECT SEQ_EMP_ID.NEXTVAL INTO :NEW.emp_id FROM DUAL;
END;
/
2. A Stored Procedure, Function, or Package References the Sequence
PL/SQL objects that call SEQUENCE.NEXTVAL or SEQUENCE.CURRVAL internally will register a dependency in the data dictionary.
-- Search source code for sequence references
SELECT name, type, line, text
FROM user_source
WHERE UPPER(text) LIKE '%SEQ_EMP_ID%'
ORDER BY name, line;
-- Example procedure causing the dependency
CREATE OR REPLACE PROCEDURE PROC_INSERT_EMP (p_name IN VARCHAR2) IS
BEGIN
INSERT INTO EMPLOYEES (emp_id, emp_name)
VALUES (SEQ_EMP_ID.NEXTVAL, p_name);
END;
/
3. A View or Materialized View References the Sequence
Although less common, views that include sequence references in their definition will also block the drop.
-- Check for views referencing the sequence
SELECT d.name AS view_name, d.type
FROM user_dependencies d
WHERE d.referenced_name = 'SEQ_EMP_ID'
AND d.referenced_type = 'SEQUENCE'
AND d.type IN ('VIEW', 'MATERIALIZED VIEW');
Quick Fix Solutions
Step 1 – Identify all dependent objects first:
-- Full dependency check (use dba_dependencies with DBA privileges)
SELECT name, type, referenced_name
FROM user_dependencies
WHERE referenced_name = 'SEQ_EMP_ID'
AND referenced_type = 'SEQUENCE'
ORDER BY type, name;
Step 2 – Remove or update dependent objects, then drop the sequence:
-- Option A: Drop the trigger, then drop the sequence
DROP TRIGGER TRG_EMP_INSERT;
DROP SEQUENCE SEQ_EMP_ID;
-- Option B: Drop the procedure, then drop the sequence
DROP PROCEDURE PROC_INSERT_EMP;
DROP SEQUENCE SEQ_EMP_ID;
-- Option C: If you need to recreate the sequence with new parameters,
-- recreate dependent objects to point to the new sequence
CREATE SEQUENCE SEQ_EMP_ID_V2
START WITH 5001
INCREMENT BY 1
NOCACHE
NOCYCLE;
-- Update the trigger to reference the new sequence
CREATE OR REPLACE TRIGGER TRG_EMP_INSERT
BEFORE INSERT ON EMPLOYEES
FOR EACH ROW
BEGIN
SELECT SEQ_EMP_ID_V2.NEXTVAL INTO :NEW.emp_id FROM DUAL;
END;
/
-- Now it is safe to drop the old sequence
DROP SEQUENCE SEQ_EMP_ID;
Prevention Tips
-
Run a dependency check before every
DROP SEQUENCEstatement. Add a validation query againstUSER_DEPENDENCIESorDBA_DEPENDENCIESas a mandatory pre-flight step in your deployment scripts or CI/CD pipelines. This ensures no silent dependencies are overlooked.
-- Pre-flight check: should return 0 rows before dropping
SELECT COUNT(*) AS dep_count
FROM user_dependencies
WHERE referenced_name = 'SEQ_EMP_ID'
AND referenced_type = 'SEQUENCE';
-- Proceed with DROP only if dep_count = 0
-
Adopt a consistent naming convention and document sequence ownership. Using a pattern like
SEQ_<TABLE>_<COLUMN>makes it immediately obvious which objects depend on a sequence. Complement this with comments in the data dictionary (COMMENT ONstatements) or a dedicated metadata table listing each sequence and its consumers — this dramatically reduces the investigation time when schema changes are needed.
Related Errors
-
ORA-02289 –
sequence does not exist: The opposite scenario where a dependent object tries to reference a sequence that has already been dropped. -
ORA-04042 –
procedure, function, package, or package body does not exist: Can occur after forcibly removing a sequence-dependent object. -
ORA-02449 –
unique/primary keys in table referenced by foreign keys: A structurally similar error for tables, resolved with the same dependency-first investigation approach.
📖 Want a more detailed guide?
Check out the full in-depth version (Korean) on oraerror.com — includes detailed analysis, additional SQL examples, and prevention tips.
Top comments (0)