If you have worked in the Oracle database ever, you would definitely have seen ORA-00904: invalid identifier error. Doesn't matter which version you are working 10g, 11g or 12g, this is one of the most common error comes while doing CRUD (Create, Read, Update, and Delete) operations in Oracle. By the way, if you are a beginner, SELECT, INSERT, UPDATE and DELETE are used to perform CRUD operations in the Oracle database. What do you do if you get this error while running in SQL script? Like any error, you should first pay attention to error message, what is Oracle trying to say here. Invalid identifier means the column name entered is either missing or invalid, this is one of the most common causes of this error but not the only one.
Sometimes it comes if you use names, which happened to be reserved word in Oracle database. Now how do you resolve it? We will learn in this article, by following series of examples which first reproduce this error and later suggest how to fix it.
In short, here is the cause and solution of "ORA-00904: invalid identifier error"
Cause: Column name in error is either missing or invalid.
Action: Enter a valid column name.
Sometimes it comes if you use names, which happened to be reserved word in Oracle database. Now how do you resolve it? We will learn in this article, by following series of examples which first reproduce this error and later suggest how to fix it.
In short, here is the cause and solution of "ORA-00904: invalid identifier error"
Cause: Column name in error is either missing or invalid.
Action: Enter a valid column name.
In the Oracle database, a valid column name must begin with a letter, be less than or equal to 30 characters, and consist of only alphanumeric characters and the special characters $, _, and #. If it contains other characters, then it must be enclosed in double quotation marks. It may not be a reserved word.
Some reasons for "ORA-00904: invalid identifier error"
If you want to understand any error, be it NullPointerException in Java or this error in Oracle, you must first know how to reproduce it.Until you know the real cause, which you would if you can reproduce it regularly, you won't be able to understand the solution.
This is why I have listed down some common scenarios where I have seen this error. Here are some examples which may lead to ORA-00904 or "invalid identifier" in Oracle 10g database.
This kind of mistake creeps in because of classic copy and paste culture.
Reason 1: Due to extra comma at last column
Yes, an extra comma at the end of create table statement can cause "ORA-00904 or "invalid identifier" . This is by far most common reason of this dreaded error and I have seen developers spent hours to find out and fixed this silly mistake.This kind of mistake creeps in because of classic copy and paste culture.
For example, if you are copying column definition from some other table's DDL statement and if the said column is not the last one you will also copy comma, and if you put it as the last column in your DDL statement you will see "ORA-00904: invalid identifier" because after comma Oracle expect another column declaration.
The interesting part is, your mind will start focusing on column names of the rest of the column and start wondering what's wrong because they all look good, and then the most developer will start doing strange things, it's hard to see that last comma in a big DDL statement with lots of column and constraints.
The interesting part is, your mind will start focusing on column names of the rest of the column and start wondering what's wrong because they all look good, and then the most developer will start doing strange things, it's hard to see that last comma in a big DDL statement with lots of column and constraints.
For example, here is how do you reproduce this error
By the way, it's easy to spot that error in simple table declaration like above, how about this table declaration
It's slightly difficult to spot commas in the last column declaration, but in real world table declaration is much much bigger with lots of constraints and column names. It's better to explicitly check the last column declaration rather than finding it while running query against database.
CREATE TABLE DBA ( ID NUMBER, NAME VARCHAR2(50), SALARY NUMBER, // ' Dont put comma at last column declaration ' );If you run this in SQLFiddle against Oracle 11g database, you will get "Schema Creation Failed: ORA-00904: : invalid identifier".
By the way, it's easy to spot that error in simple table declaration like above, how about this table declaration
CREATE TABLE Items ( itemId NUMBER(10), CONSTRAINT primary_pk PRIMARY KEY (itemId), itemname VARCHAR2(100), catogoryId NUMBER(10), CONSTRAINT subcategory_fk FOREIGN KEY (catogoryId ) REFERENCES itemSubCategory(catogoryId ), companyId VARCHAR2(20), CONSTRAINT company_fk FOREIGN KEY(companyId ) REFERENCES CompanyInfo(companyId ), description VARCHAR2(1000), supplierId VARCHAR2(20), CONSTRAINT supplier_fk FOREIGN KEY(supplierId ) REFERENCES SupplierInfo(supplierId ), price FLOAT, quantity NUMBER(10), );
It's slightly difficult to spot commas in the last column declaration, but in real world table declaration is much much bigger with lots of constraints and column names. It's better to explicitly check the last column declaration rather than finding it while running query against database.
Reason 2 : Due to the Reserved keyword as Column name
CREATE TABLE DBA ( ID NUMBER, NAME VARCHAR2(50), AUDIT VARCHAR2(1000) );If you run following query at SQLFiddle (a website where you can try SQL query online on any database) you will see the error Schema Creation Failed: ORA-00904: : invalid identifier.
The reason our schema creation failed because AUDIT is a reserved word in Oracle 11g R2.
Unfortunately SQLFiddle doesn't give more details like SQLDeveloper, Toad or any command line tool like Oracle SQL Plus client e.g. if you run the same example in SQL client, you will see something like :
It's much easier to find out the culprit in this case, as you have line number and Oracle is giving you enough hint that AUDIT is an invalid identifier.
SQL> CREATE TABLE DBA 2 ( 3 ID NUMBER, 4 NAME VARCHAR2(50), 5 AUDIT VARCHAR2(1000) 6 ); AUDIT VARCHAR2(1000) * ERROR at line 5: ORA-00904: invalid identifier
It's much easier to find out the culprit in this case, as you have line number and Oracle is giving you enough hint that AUDIT is an invalid identifier.
It doesn't tell you explicitly that it's a reserved keyword.
By the way, you don't need to know all reserved keyword on top of your head, you can also ways look at the following link (http://docs.oracle.com/cd/E11882_01/server.112/e26088/ap_keywd001.htm#SQLRF55621) to see if that "invalid identifier" error is due to the reserved keyword.
By the way, you don't need to know all reserved keyword on top of your head, you can also ways look at the following link (http://docs.oracle.com/cd/E11882_01/server.112/e26088/ap_keywd001.htm#SQLRF55621) to see if that "invalid identifier" error is due to the reserved keyword.
Some of the keywords which developers often mistakenly use as column names are COMMENT, CHECK, EXCLUSIVE, INITIAL, LEVEL, ONLINE, PRIOR, RESOURCE, SHARE, and SUCCESSFUL.
ORA-00904: invalid identifier While Inserting data into Table
Apart from table creation, you will see error "ORA-00904: invalid identifier" if you use the wrong column name in an INSERT statement or use a non-existent column name.Most of the time it happens because of typo, but some other time it could be due to a parallel update like someone changed the schema of the table and renamed or dropped the column you are referring to in the INSERT query.
Here is an example of ORA-00904: invalid identifier while inserting data into a table
You can see that Oracle database complains about "DEPT_ID" column as an invalid identifier because there is no such column exists in our DBA table.
and you try to execute following SQL SELECT Query :
SQL> insert into DBA values (102, 'Mohan', 10500); //Ok SQL> insert into DBA(ID, NAME, SALARY) values (101, 'John', 10000); //Ok SQL> insert into DBA(ID, NAME, SALARY, DEPT_ID) values (101, 'John', 10000, 1); // Not Ok ORA-00904: "DEPT_ID": invalid identifier : insert into DBA(ID, NAME, SALARY, DEPT_ID) values (101, 'John', 10000, 1)
You can see that Oracle database complains about "DEPT_ID" column as an invalid identifier because there is no such column exists in our DBA table.
ORA-00904: invalid identifier due to accessing non-existing column in SELECT
This is the obvious one, if you try to access an invalid column from a table in SELECT query, you will get ORA-00904: invalid identifier. For example, if you have following table :CREATE TABLE DBA ( ID NUMBER, NAME VARCHAR2(50), SALARY NUMBER );
and you try to execute following SQL SELECT Query :
SQL> SELECT DEPT_ID FROM DBA;You will get following error "ORA-00904: "DEPT_ID": invalid identifier" because there is no DEPT_ID column in DBA table.
ORA-00904: invalid identifier error because or wrong column name in UPDATE query
Just like previous example, you will get this error if you use wrong or non-existing column name in your UPDATE statement. In following example, we are trying DEPT_ID column which doesn't exists in DBA table, that's why ORA-00904: invalid identifier errorSQL> UPDATE DBA set DEPT_ID=1 where ID=101; ORA-00904: "DEPT_ID": invalid identifier : UPDATE DBA set DEPT_ID=1 where ID=101You can see that error nicely point out that DEPT_ID is invalid column.
Reason 5: Due to incorrect column name in DELETE query
Similar to the previous example of SELECT and UPDATE query, you will also face "ORA-00904: invalid identifier" if you give the wrong column name in DELETE statements.It could be due to a typo or because of a recent update in the schema which dropped the column you are using in your DELETE clause.
You can see that Oracle gives you a hint that "DEPT_ID" is an invalid identifier because there is no such column in the DBA table.
If you are preparing SQL script to run on the production database, make sure you test these queries on a production copy of the database before running it directly on live database.
SQL> DELETE FROM DBA WHERE ID=101; // Ok SQL> DELETE FROM DBA WHERE DEPT_ID=1; // Not Ok, ORA-00904: invalid identifier ORA-00904: "DEPT_ID": invalid identifier : delete from DBA where DEPT_ID=1
You can see that Oracle gives you a hint that "DEPT_ID" is an invalid identifier because there is no such column in the DBA table.
How to Avoid Invalid Identifier Error in Oracle database?
ORA-00904 can simply be avoided by using the valid column name in DDL like CREATE or ALTER statement. Also for DML statements like SELECT, UPDATE, INSERT and DELETE, ORA-00904 can be avoided by using the correct column name and doing four eye checks to catch any typo.If you are preparing SQL script to run on the production database, make sure you test these queries on a production copy of the database before running it directly on live database.
You should also have process to do four eye check and review to avoid such errors.
Similarly, if you are creating a table make sure you use a valid column name in your schema. A valid column name in Oracle database
That's all about how to fix ORA-00904: invalid identifier error in the Oracle 11g database. ORA-00904 is a very simple issue. ORA-00904 may occur when we try to create or alter a table with the invalid column name.
Similarly, if you are creating a table make sure you use a valid column name in your schema. A valid column name in Oracle database
- Must begin with a letter.
- Can not be of more than 30 characters.
- Must be made up of alphanumeric characters
- May contain following special characters: $, _, and #.
- If the column name uses any other characters, it must be enclosed in double quotation marks.
- Can not be a reserved word.
It also may occur when we try to reference a non-existing column in a select / insert/update/delete statement.
So just remember the tips and solution we have shared here, it will help you to quickly troubleshoot and fix this error.
You can use reserved words at column name with double quote like :
ReplyDeletehttp://sqlfiddle.com/#!4/bf3ec
I think you can also use reserved keyword in column name by enclosing in on bracket e.g. [case], this works in SQL Server but I am not sure if it works in Oracle or not.
ReplyDeleteI have created a FUNCION where I sending a number and the function of returns a DATE
ReplyDeleteFUNCTION F_ORDEN_LINEAS (TELEFONO IN VARCHAR2) RETURN DATE IS
/* 08-05-2015
CON ESTA FUNCION CAMBIAMOS EL ORDER BY DE LINEAS, PARA QUE SIEMPRE SEAN
MOSTRADAS
COMO PRIMERAS LINEAS LAS AUTORIZADAS Y DESPUES LAS NO AUTORIZADAS
*/
FECHA DATE := NULL;
BEGIN
SELECT B.FINCONTRA
INTO FECHA
FROM ABONOS B
WHERE B.TELEFONO = TELEFONO
AND B.CUENTA = :B_DURCONTRA.COD_CUENTA
AND B.BAJA = 'N'
AND B.ABOSITUA NOT IN ('SA','SP','BT','BP')
AND NOT EXIST (SELECT A.TELEFONO
FROM DATOSGEN B, CAMBDURACONT Z
WHERE Z.TELEFONO = B.TELEFONO
AND Z.CUENTA = :B_DURCONTRA.CUENTA
AND Z.MODIFICACION >= B.INIPERVAL
AND Z.MODIFICACION < (B.FINPERVAL+1)
AND Z.RECUPERACION = 'N')
UNION
SELECT A.MODIFICACION
FROM DATOSGEN B, CAMBDURACONT A
WHERE A.TELEFONO = TELEFONO
AND A.CUENTA = :B_DURCONTRA.CUENTA
AND A.MODIFICACION >= B.INIPERVAL
AND A.MODIFICACION < (B.FINPERVAL+1)
AND A.RECUPERACION = 'N';
RETURN FECHA;
EXCEPTION
WHEN OTHERS THEN
FECHA := SYSDATE;
RETURN FECHA;
END;
And this FUNCTION, it is called from SET_BLOCK_PROPERTY('B_LINEAS',ORDER_BY,'F_ORDEN_LINEAS(TELEFONO)');
But always the function of returns the mistake ORA-00904
Please help me
I am getting the same error when select nxt day of a date field from a table. Please help me to resolve this
ReplyDeleteSelect MAX(TO_CHAR(TO_DATE(CONVERSION_DATE,"YYYY-MM-DD") + 1,"YYYYMMDD")) FROM EMPLOYEE;
Select MAX(TO_CHAR(TO_DATE(CONVERSION_DATE,"YYYY-MM-DD") + 1,"YYYYMMDD")) FROM EMPLOYEE^J *^JERROR at line 1:^JORA-00904: "YYYYMMDD": invalid identifier: syntax error
Select * from dregist where dname=Request.QueryString ("q");
ReplyDeleteThere is invalid identifier ora-00904 error