ORA-00904 when inserting/querying data from tables - sql

I'm getting the flowing error:
Error starting at line 1 in command:
INSERT INTO driver (registration, make, model, gvw, year, body) VALUES('4585 AW','ALBION','RIEVER',20321,1963, ' ');
Error at Command Line:1 Column:53
Error report:
SQL Error: ORA-00904: "BODY": invalid identifier
00904. 00000 - "%s: invalid identifier"
When I do the following
INSERT INTO driver
(registration, make, model, gvw, year)
VALUES
('4585 AW','ALBION','RIEVER',20321,1963, ' ');
So I temporally deleted the body data and then give the error
Error starting at line 1 in command:
INSERT INTO driver (registration, make, model, gvw, year) VALUES('4585 AW','ALBION','RIEVER',20321,1963)
Error at Command Line:1 Column:53
Error report:
SQL Error: ORA-00904: "YEAR": invalid identifier
00904. 00000 - "%s: invalid identifier"
I have columns both called year and body, yet I'm getting errors. How can I fix this?

Seems like you specified the column names under quotes in your DDL.
Oracle considers identifiers under quotes as a case sensitive, so creating a table with a column name as "body" or "year" will not resolve body or year in your query, because when you omit the quotes, it considers the upper case variant.
So when you have this:
create table driver_case (registration varchar2(60),
make varchar2(60),
model varchar2(60),
gvw number,
"year" number,
"body" varchar2(60));
and then try to do
select year, body from driver_case
then Oracle tries to find column "YEAR", "BODY" (remember, without an identifier without quotes gets converted to upper case) which is not the same as "year", "body" in your table.
Your solutions?
Don't mention column names under quotes in the DDL
If you disregard the above point, then you must mention the column names under quotes in all your DML statements.
I demonstrate the above point in this SQL Fiddle

Try this:
INSERT
WHEN city='palanpur' THEN INTO Ashish
When city='Aburoad' THEN INTO abhi
SELECT no, name
FROM arpit;

Related

Invalid datatype for range function

I am trying to use range partition on a table for a column of type number. However, Oracle 12c throws an error saying it is an invalid data-type. I don't understand why/what is invalid in the command.
create table partition_tester (
some_column number not null,
another_column varchar2(10),
partition by range(some_column) interval(10)
);
I am connecting to the database using SQL developer. It seems to have issue with the range function. On executing above script it throws:
Error report -
ORA-00902: invalid datatype
00902. 00000 - "invalid datatype"
*Cause:
*Action:
Any help is appreciated.
remove the comma after " another_column varchar2(10)," and specify at least one partition.
create table partition_tester (
some_column number not null,
another_column varchar2(10))
partition by range(some_column) interval(10)
(
partition p0 values less than (10)
)

getting invalid number when aggregating varchar using to_number

I have the below query
select avg(to_number(pst_adm_scr)) from dmereg01.tst_scr
I keep getting the error:
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.
The field is originally a varchar with numeric values (scores)
Depending on your database version (which you haven't specified), you may be able to use the ON CONVERSION ERROR component of TO_NUMBER to ignore the invalid values
SELECT TO_NUMBER('-' DEFAULT null ON CONVERSION ERROR)
from dual;

Why am I getting ORA-00904: invalid identifier for deterministic function index?

create or replace function idxF(status IN char) return char deterministic is
retVal CHAR(1);
begin
dbms_output.put_line('P');
retVal:=CASE status
when 'P' then 'P'
when 'H' then 'H'
else null
end ;
return retVal;
end idxF;
create index setIndexOnStatus on ORDER(idXF(STATUS));
SQL Error: ORA-00904: "IDXF": invalid identifier
STATUS is a CHAR(1) column in ORDER table.
Why is the compiler saying that IDXF is an invalid identifier?
ORDER is a reserved word in Oracle (part of ORDER BY clause) so you shouldn't use it as a name for your table.
Technically, you can escape a reserved word with double quotes:
create index setIndexOnStatus on "ORDER"(idXF(STATUS));
Probably, this table was created like this in a first place.
This approach is extremely problematic because you'll need to escape every query to this table and it's not always possible, especially with auto-generated queries.
So, don't go this way, rename the table instead.
P.S. I couldn't reproduce ORA-00904 on this query. Invalid table name leads to ORA-00903: invalid table name. Escaped query works fine for me. If you still have ORA-00904 error then you'll need to build a reproducible example to demonstrate your problem (i.e. include CREATE TABLE statement and specify the Oracle version).

ORA-00904 while converting UTC date to Oracle date

I wrote the following query in Oracle, and it is working fine in one schema but giving an ORA-00904 error for the utc_to_oracle converter function in another schema.
SELECT MIN(to_char(utc_to_oracle(start_t),'DD-MON-YYYY hh24:mi:ss')),
MAX(to_char(utc_to_oracle(end_t),'DD-MON-YYYY hh24:mi:ss'))
FROM ts.udata
WHERE entity_type='wxyz'
Error:
ORA-00904: "UTC_TO_ORACLE": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action: Error at Line: 2 Column: 13
Why is it working on one schema but not the other?
ORA-00904 occurs when you reference an invalid object.
You need to provide grants for the user defined object UTC_TO_ORACLE to the target user to have this executed.
GRANT SELECT ON UTC_TO_ORACLE TO XXX;

Column Name beginning with a number?

I have a column name in one of my tables called: 3RD_DIAG_CODE - VARCHAR2 (10 Byte)
When I try to run a query, it gives me the following error highlighting 3RD_DIAG_CODE.
ORA-00923: FROM keyword not found where expected.
How can I bring this field in without it throwing an error every time I bring this field in?
If you are using column names that start with a number then you need to use double quotes. For example:
create table foo (
"3RD_DIAG_CODE" varchar2(10 byte) --make sure you use uppercase for variable name
);
insert into foo values ('abc');
insert into foo values ('def');
insert into foo values ('ghi');
insert into foo values ('jkl');
insert into foo values ('mno');
commit;
select * from foo;
3RD_DIAG_C
----------
abc
def
ghi
jkl
mno
select 3RD_DIAG_CODE from foo;
RD_DIAG_CODE
------------
3
3
3
3
3
select "3RD_DIAG_CODE" from foo;
3RD_DIAG_C
----------
abc
def
ghi
jkl
mno
Edit: As for the error message itself, you are probably (as BQ wrote) missing a comma from the select clause.
Check your specification, but in SQL Server we would have to enclose that column name in square brackets: [3RD_DIAG_CODE]
You probably have two columns listed without a comma between them.
create table t (id number primary key, 3d varchar2(30))
Error at Command Line:1 Column:39
Error report:
SQL Error: ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
create table t (id number primary key, "3d" varchar2(30));
table T created.
desc t
Name Null Type
---- -------- ------------
ID NOT NULL NUMBER
3d VARCHAR2(30)
> select id, 3d from t --[as #gsiem mentions: THIS IS BAD]
ID 3D
---------------------- --------
> select id, "3d" from t
ID 3d
---------------------- ------------------------------
> select id, [3d] from t
Error starting at line 7 in command:
select id, [3d] from t
Error at Command Line:7 Column:11
Error report:
SQL Error: ORA-00936: missing expression
00936. 00000 - "missing expression"
*Cause:
*Action:
> select id 3d from t
Error starting at line 8 in command:
select id 3d from t
Error at Command Line:8 Column:10
Error report:
SQL Error: ORA-00923: FROM keyword not found where expected
00923. 00000 - "FROM keyword not found where expected"
*Cause:
*Action: