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.
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
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;
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;
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;