Apprpriate usage of whenever sqlerror - sql

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.

Related

Why am I receiving the PSL-00103 Error in Oracle SQL?

I am trying to make a procedure that would insert a row into a table in Oracle SQL. However, I can't figure out a solid reason on why this issue exists when I write any type of procedure.
I have tried changing the syntax around a couple of times, but I still don't know how to remedy the issue.
Errors given:
Error at line 1: PLS-00103: Encountered the symbol ")" when expecting one of the following: in out table ... columns long double ref char time timestamp interval date binary national character nchar
Error at line 5: PL/SQL: SQL Statement ignored Error at line 6: PL/SQL: ORA-00984: column not allowed here
Code:
create or replace procedure insert_category(
category_name_param in categories.category_name%type)
as
begin
insert into categories (category_id, category_name)
values (category_id, category_name_param);
end;
It seems that procedure lacks in yet another parameter; see if this helps (I presumed that such a column exists in the table; can't be sure as you didn't post table description):
create or replace procedure insert_category(
category_name_param in categories.category_name%type,
category_id_param in categories.category_id%type --> this
)
as
begin
insert into categories
(category_id,
category_name)
values
(category_id_param, --> this
category_name_param);
end;

Why is the table not able to be selected/viewed in sql plus? [FIXED]

I tried to create a trigger.
This is the code I used
create or replace trigger prod_se15
after insert on product_se15
for each row
begin
insert into product1_se15#link4bomb2 values(:new.pro_id,:new.pro_name,:new.pro_desc);
insert into product2_se15#link4bomb3 values(:new.pro_id,:new.pro_cost,:new.pro_profit);
end;
/
The error
2/3 PL/SQL: ORA-00942: table or view does not exist
2/3 PL/SQL: SQL Statement ignored
3/3 PL/SQL: ORA-00942: table or view does not exist
3/3 PL/SQL: SQL Statement ignored
Note:
The table is displayed when desc product_se15 or select * from product_se15 command is executed.
Tried to grant the privileges method via another user but it did not work.
Fixed
Detailed Error
: LINE/COL ERROR
-------- -----------------------------------------------------------------
0/0 ORA-04052: error occurred when looking up remote object
SCOTT.PRODUCT1_SE15#LINK4BOMB2
ORA-00604: error occurred at recursive SQL level 1
ORA-01017: invalid username/password; logon denied
ORA-02063: preceding line from LINK4BOMB2
#The password string mismatch

Error while executing alter table to add new column with default values as seq.nextval

I'm getting below error while executing the following sql query with Oracle 11g
ALTER TABLE table add column_col integer DEFAULT table_seq.nextval not null;
Error report -
ORA-00984: column not allowed here
00984. 00000 - "column not allowed here"
That syntax doesn't work with 11g. It's new to 12. You need a trigger:
CREATE OR REPLACE TRIGGER "TRIGGERNAME" BEFORE INSERT ON <<TABLENAME>>
FOR EACH ROW
WHEN (new."COLUMNNAME" IS NULL)
BEGIN
SELECT table_seq.nextval
INTO :new."COLUMNNAME"
FROM dual;
END;
/

SEM_MATCH call returns ORA-00902: invalid datatype

When trying to run examples from http://docs.oracle.com/cd/E11882_01/appdev.112/e25609/sem_apis_ref.htm everything is ok except the most important call of SPARQL query. Here's the shortened version of the example "family" script:
CREATE TABLE family_rdf_data (id NUMBER, triple SDO_RDF_TRIPLE_S);
EXECUTE SEM_APIS.create_sem_model('family', 'family_rdf_data', 'triple');
INSERT INTO family_rdf_data VALUES (15,
SDO_RDF_TRIPLE_S('family',
'<http://www.example.org/family/Tom>',
'<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>',
'<http://www.example.org/family/Male>'));
COMMIT;
-- Select all males from the family model, without inferencing.
SELECT m
FROM TABLE(SEM_MATCH(
'{?m rdf:type :Male}',
SEM_Models('family'),
null,
SEM_ALIASES(SEM_ALIAS('','http://www.example.org/family/')),
null));
SQLPLUS output:
Table created.
PL/SQL procedure successfully completed.
1 row created.
Commit complete.
SELECT m
*
ERROR at line 1:
ORA-00902: invalid datatype
All similar calls from these examples fail with the same error. Any ideas?
The issue has been resolved with reinstallation of Oracle database from scratch including Spatial Option from the start.

Why does this basic 'Select Into' stored procedure not work?

I'm running Oracle SQL developer and I've got the following Stored Procedure. I'm quite new to this but really not sure why this isn't working:
CREATE OR REPLACE PROCEDURE CHECKDUPLICATE(
username1 IN USERS.USERNAME%TYPE,
o_username OUT USERS.USERNAME%TYPE
)
IS
BEGIN
SELECT USERNAME
INTO o_username
FROM USERS WHERE username1 = o_username;
END;
When I try to call it:
DECLARE
o_username USERS.USERNAME%TYPE;
BEGIN
CHECKDUPLICATE('Jacklin', o_username);
DBMS_OUTPUT.PUT_LINE('username : ' || o_username);
END;
I get the error message:
Error starting at line 1 in command:
DECLARE
o_username USERS.USERNAME%TYPE;
BEGIN
CHECKDUPLICATE(Jacklin, o_username);
DBMS_OUTPUT.PUT_LINE('username : ' || o_username);
END;
Error report:
ORA-06550: line 5, column 19:
PLS-00201: identifier 'JACKLIN' must be declared
ORA-06550: line 5, column 4:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
What does it mean by "Identifier 'Jacklin' must be declared? (Table is called USERS, and column name is called USERNAME). Any help would be appreciated.
EDIT** I put Jacklin in quotes, and I get this message now:
Error report:
ORA-01403: no data found
ORA-06512: at "L13JAV04.CHECKDUPLICATE", line 9
ORA-06512: at line 6
01403. 00000 - "no data found"
*Cause:
*Action:
Even though Jacklin does it exist in the database!
Once you quote 'Jacklin' so that it's treated as a string literal rather than an identifier, your SQL statement doesn't look right.
SELECT USERNAME
INTO o_username
FROM USERS
WHERE username1 = o_username;
My wager is that you want to use the input parameter in your WHERE clause, not the output parameter.
SELECT USERNAME
INTO o_username
FROM USERS
WHERE username1 = username;
It doesn't make sense to check the value of an output parameter when you haven't done anything to initialize it.
But your code still doesn't seem to make sense. A SELECT INTO will throw an error if anything other than 1 row is returned. If your query returns 0 rows, you'll get a NO_DATA_FOUND exception. If your query returns more than 1 row, you'll get a TOO_MANY_ROWS exception. Your procedure is named CheckDuplicate so I'm guessing that it's purpose is to check whether a particular username already exists in the table rather than trying to insert it and catching the unique constraint violation error. If that is the intention of your code
You probably want it to be a function
You probably don't want to return the username
You probably want to return an indicator of whether the username already exists
My guess, therefore, is that you would want something like
create or replace function isAvailable( p_username IN USERS.USERNAME%TYPE )
return Boolean
is
l_username USERS.USERNAME%TYPE;
begin
select username
into l_username
from users
where username = p_username;
return false;
exception
when no_data_found
then
return true;
end;
You need to put Jacklin within quotes for it to be treateed as a string. Otherwise the parser thinks it's a field name.
There would have been no user by name of "Jacklin" thats why its giving you the error. Please add an exception at the end
WHEN NO_DATA_FOUND
THEN
......