Sql error "invalid identifier" - sql

Anyone idea why am I getting error ?
create or replace view PROD as
select
mytable.name as description
from table_name mytable
join another_table another on another.id in (
select mytable1.id
from table_name mytable1
start with mytable1.id = mytable.id
connect by prior mytable1.id = mytable1.parent_id);
Error at Command Line:8 Column:41
Error report:
SQL Error: ORA-00904: "MYTABLE"."ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
This actually works for my local database installation. However, not on any other. Running on Oracle database.
Thank you

Version problem:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 -
Production
Oracle Database 12c Standard Edition Release 12.1.0.2.0 - 64bit
Production
Where 1. is the one where this syntax does not work.

Related

Apprpriate usage of whenever sqlerror

Correct usage of whenever sqlerror in oracle.
Can we have examples.
Docs says: Performs the specified action (exits SQLPlus by default) if a SQL command or PL/SQL block generates an error.*
Nothing easier than an example:
script without WHENEVER clause
Create a script (script1.sql) with the following lines. Note the error in the CREATE TABLE statement for tab1 - this statement will error out.
koens macbook % cat script1.sql
prompt first statement
CREATE TABLE tab1 (col1 NUMBERX);
prompt second statement
CREATE TABLE tab2 (col2 NUMBER);
Now run this script - I'm using sqlcl as client:
18c>#script1
first statement
Error starting at line : 3 File # /Users/klostrie/tmp/script1.sql
In command -
CREATE TABLE tab1 (col1 NUMBERX)
Error report -
ORA-00902: invalid datatype
00902. 00000 - "invalid datatype"
*Cause:
*Action:
second statement
Table TAB2 created.
18c>
Note that the first statement fails as expected but the script continues with the next statement
script with WHENEVER clause
Now lets try the same using WHENEVER SQLERROR
18c>!cat script2.sql
whenever sqlerror exit sql.sqlcode
prompt first statement
CREATE TABLE tab1 (col1 NUMBERX);
prompt second statement
CREATE TABLE tab2 (col2 NUMBER);
18c>#script2
first statement
Error starting at line : 4 File # /Users/klostrie/tmp/script2.sql
In command -
CREATE TABLE tab1 (col1 NUMBERX)
Error report -
ORA-00902: invalid datatype
00902. 00000 - "invalid datatype"
*Cause:
*Action:
Disconnected from Oracle Database 18c EE High Perf Release 18.0.0.0.0 - Production
Version 18.7.0.0.0
koens macbook %
See the difference ? Because of the WHENEVER SQLERROR EXIT the script stops after the first error and exits the oracle client (sqlcl) with the actual error code (ORA-902)
Check the docs for other arguments of WHENEVER SQLERROR.

Invalid input values for pname while trying to get the table preference

here is the sql query i am trying to run to get the TABLE_CACHED_BLOCKS preference
select dbms_stats.get_prefs ( pname=>'TABLE_CACHED_BLOCKS', ownname=>user, tabname=>'bricks_idx1' ) from dual;
I am getting the following error message
ORA-20001: Invalid input values for pname
help me in finding the solution ..
I am using Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

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;

ORA-00904: Invalid identifier when using dbms_random.value

This article here suggests using dbms_random.value to select random values from a table.
My query
select value1, value2 from
(select value1, value2 from my.table order by dbms_random.value)
where rownum <100;
Gives me
Error at Command Line:2 Column://column after the 'by').
Error report:
SQL Error: ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
Any suggestions?
Insufficient user priviledges. Don't have access to the dbms_random package.
You need to GRANT EXECUTE to your schema. Login as your SYS user and run the following, replacing 'your-schema-name' with the Oracle user who you wish to allow run DBMS_RANDOM.
GRANT EXECUTE ON DBMS_RANDOM TO your-schema-name;

ORA-00904 when inserting/querying data from tables

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;