DEV Community

umzzil nng
umzzil nng

Posted on Originally published at oraerror.com

Oracle ORA-04012 Error: Causes and Solutions Complete Guide

ORA-04012: Object Is Not a Sequence

ORA-04012 is thrown by Oracle Database when you attempt to use sequence-specific pseudocolumns — NEXTVAL or CURRVAL — on a database object that is not a sequence (e.g., a table, view, or synonym pointing to a non-sequence object). Oracle performs an internal type check on the referenced object, and when it detects a mismatch, it raises this error immediately. Understanding the root cause quickly is essential because this error can surface in production applications and block critical insert or ID-generation workflows.


Top 3 Causes and Fixes

1. Typo or Wrong Object Name Referenced as a Sequence

The most common cause is simply referencing the wrong object name — for example, calling .NEXTVAL on a table name instead of the intended sequence.

Diagnosis:

-- Check what type of object exists with that name
SELECT OBJECT_NAME, OBJECT_TYPE, STATUS
FROM USER_OBJECTS
WHERE OBJECT_NAME = 'ORDERS_SEQ';

-- List all sequences to find the correct name
SELECT SEQUENCE_NAME, LAST_NUMBER
FROM USER_SEQUENCES
ORDER BY SEQUENCE_NAME;
Enter fullscreen mode Exit fullscreen mode

Fix:

-- If the sequence doesn't exist, create it
CREATE SEQUENCE ORDERS_SEQ
    START WITH 1
    INCREMENT BY 1
    NOCACHE
    NOCYCLE;

-- Use it correctly
INSERT INTO ORDERS (ORDER_ID, ORDER_DATE)
VALUES (ORDERS_SEQ.NEXTVAL, SYSDATE);
Enter fullscreen mode Exit fullscreen mode

2. Synonym Pointing to a Non-Sequence Object

A public or private synonym intended to reference a sequence may have been incorrectly created or accidentally reassigned to a table or view. This is particularly tricky to debug because the synonym name looks correct at first glance.

Diagnosis:

-- Check what object the synonym actually points to
SELECT SYNONYM_NAME, TABLE_OWNER, TABLE_NAME
FROM USER_SYNONYMS
WHERE SYNONYM_NAME = 'ORDER_SEQ_SYN';

-- Cross-check the target object type
SELECT OBJECT_TYPE, STATUS
FROM DBA_OBJECTS
WHERE OWNER = 'APP_SCHEMA'
  AND OBJECT_NAME = 'ORDERS_SEQ';
Enter fullscreen mode Exit fullscreen mode

Fix:

-- Drop the broken synonym and recreate it correctly
DROP SYNONYM ORDER_SEQ_SYN;

CREATE SYNONYM ORDER_SEQ_SYN
    FOR APP_SCHEMA.ORDERS_SEQ;

-- Verify it works
SELECT ORDER_SEQ_SYN.NEXTVAL FROM DUAL;
Enter fullscreen mode Exit fullscreen mode

3. Schema Confusion in Multi-Schema Environments

When multiple schemas contain objects with the same name, omitting or misspecifying the schema prefix can cause Oracle to resolve the object to a table or view in a different schema instead of the intended sequence.

Diagnosis:

-- Find all objects named ORDERS_SEQ across all schemas
SELECT OWNER, OBJECT_NAME, OBJECT_TYPE, STATUS
FROM DBA_OBJECTS
WHERE OBJECT_NAME = 'ORDERS_SEQ'
ORDER BY OWNER;

-- Check current session schema
SELECT SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA') FROM DUAL;
Enter fullscreen mode Exit fullscreen mode

Fix:

-- Always qualify with the correct schema
SELECT APP_SCHEMA.ORDERS_SEQ.NEXTVAL FROM DUAL;

-- Or switch the session schema explicitly
ALTER SESSION SET CURRENT_SCHEMA = APP_SCHEMA;

SELECT ORDERS_SEQ.NEXTVAL FROM DUAL;
Enter fullscreen mode Exit fullscreen mode

Quick Fix Checklist

  1. Run SELECT * FROM USER_SEQUENCES WHERE SEQUENCE_NAME = '<your_name>'; — if no rows return, the sequence doesn't exist or you have a typo.
  2. Check USER_SYNONYMS if you are using synonym-based access.
  3. Always prefix sequence calls with the schema name in multi-schema environments.
  4. Confirm object type with USER_OBJECTS before assuming something is a sequence.

Prevention Tips

  • Enforce naming conventions: Always suffix sequence objects with _SEQ (e.g., ORDERS_SEQ). This prevents accidental name collisions with tables or views.
  • Add pre-validation in stored procedures: When using sequences dynamically, validate the object type via USER_SEQUENCES before executing, and raise a meaningful application error instead of letting ORA-04012 surface unexpectedly.
-- Defensive sequence validation snippet
DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(*) INTO v_count
    FROM USER_SEQUENCES
    WHERE SEQUENCE_NAME = 'ORDERS_SEQ';

    IF v_count = 0 THEN
        RAISE_APPLICATION_ERROR(-20001, 'ORDERS_SEQ is not a valid sequence.');
    END IF;
END;
/
Enter fullscreen mode Exit fullscreen mode

Related Errors

Error Code Description
ORA-02289 Sequence does not exist at all
ORA-08004 Sequence NEXTVAL exceeds MAXVALUE (NOCYCLE)
ORA-04006 Invalid START WITH value during sequence creation

Key distinction: ORA-02289 means the object doesn't exist at all; ORA-04012 means the object exists but is the wrong type.


📖 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)