how to provide error message when rising oracle predefined exception - sql

I rise DUP_VAL_ON_INDEX in SQL and I'd like to associate some custom error message with it.
Right now after executing the code
IF ___SOME_CONDITION___ THEN
RAISE DUP_VAL_ON_INDEX;
END IF
I see following message:
00001. 00000 - "unique constraint (%s.%s) violated"
How I can provide custom message to substitute these "%s"?

You wouldn't typically raise these pre-defined errors yourself. Instead, you would either let the database raise them (eg. you really have tried to insert a row that violates the unique constraint/index) or raise your own custom error (eg. raise_application_error()) and provide the necessary information there.

Related

SQL Insert Statement causing error in Oracle

I'm trying to do this
INSERT INTO INSPECTIONOFFICE (INSPOFFICE_ID, INSPOFFICE_NAME, INSPOFFICE_STREET, INSPOFFICE_CITY, INSPOFFICE_STATE, INSPOFFICE_ZIPCODE, INSPOFFICE_CONTPERSONFNAME, INSPOFFICE_CONTPERSONLNAME, INSPOFFICE_CONTPERSONPHONE, INSPOFFICE_CONTPERSONEMAIL)
VALUES (12, 'InspectionOffice1', '121 Ames st', 'Omaha', 'NE', 68128, 'Brad', 'Foley', '4022853487', 'BradFoley#gmail.com');
but I get an error
Error starting at line : 22 in command -
INSERT INTO INSPECTIONOFFICE (INSPOFFICE_ID, INSPOFFICE_NAME, INSPOFFICE_STREET, INSPOFFICE_CITY, INSPOFFICE_STATE, INSPOFFICE_ZIPCODE, INSPOFFICE_CONTPERSONFNAME, INSPOFFICE_CONTPERSONLNAME, INSPOFFICE_CONTPERSONPHONE, INSPOFFICE_CONTPERSONEMAIL)
VALUES (12, 'InspectionOffice1', '121 Ames st', 'Omaha', 'NE', 68128, 'Brad', 'Foley', '4022853487', 'BradFoley#gmail.com')
Error at Command Line : 22 Column : 13
Error report -
SQL Error: ORA-04098: trigger 'JMEEK.INSPECTIONOFFICE_INSPECTIONOFF' is invalid and failed re-validation
04098. 00000 - "trigger '%s.%s' is invalid and failed re-validation"
*Cause: A trigger was attempted to be retrieved for execution and was found to be invalid. This also means that compilation/authorization failed for the trigger.
*Action: Options are to resolve the compilation/authorization errors, disable the trigger, or drop the trigger.
Any idea how to fix this?
seems you do not have permission access to the trigger JMEEK.INSPECTIONOFFICE_INSPECTIONOFF
or have some compilation problem with it...
you should take a look in the trigger.
or if you don't know what a trigger is, call a experienced job's teammate. :)
btw, if you are alone, and it don't matter too much.. you can disable / drop the trigger..
this command disable, you can enable later
ALTER TRIGGER JMEEK.INSPECTIONOFFICE_INSPECTIONOFF DISABLE;
this command drop the trigger, you will lose it forever (if you don't have a backup)
drop trigger JMEEK.INSPECTIONOFFICE_INSPECTIONOFF;
The trigger JMEEK.INSPECTIONOFFICE_INSPECTIONOFF, which I am guessing you wrote because it's in the JMEEK schema, which (again I'm guessing) seems to be yours, is invalid, meaning it has compilation errors. To find out what those errors are you should execute the query
SELECT *
FROM ALL_ERRORS
WHERE OWNER = 'JMEEK' AND
NAME = 'INSPECTIONOFFICE_INSPECTIONOFF'
ORDER BY SEQUENCE
None of us can do much else for you because we don't know what this trigger looks like. If you need to see the source code you can execute the following query:
SELECT LINE, TEXT
FROM ALL_SOURCE
WHERE OWNER = 'JMEEK' AND
NAME = 'INSPECTIONOFFICE_INSPECTIONOFF'
ORDER BY LINE
Or you can use an IDE such as Oracle's free SQL Developer product which will make all this a lot easier for you.

apex addError - remove default error message

I have added addError to my record like in the following.
v.addError('My Error message');
But I get the error message like in the following.
I don't want the default part "Error: Invalid Data. Review all error messages to correct your data".
I tried adding to some field instead of adding it to the whole record.
But in this case, it implies that the error is specific to what the user has entered in that field. But this is not what I want. My error is record specific.
In a nutshell, based on some condition, I want to stop a record being inserted. This is actually to be added to before insert of my trigger.
Please help.
Try this Code for field level error message:
trigger AccountTrigger on Account (before insert) {
for(Account accIns :trigger.new){
if(accIns.Rating == 'Hot'){
accIns.Rating.addError('My Error message');
}
}
}

How to Call package successfully

got error when calling package
error is
Error starting at line : 1 in command -
PKG_Generate_GRNo.GenerateGR(TO_NUMBER(:P164_APP_ID,
'9999999'),:APP_USER,:P164_FIRST_NAME,:P164_LAST_NAME,:P164_EMAIL,:P164_SKYPE_ID,:P164_COUNTRY,:P164_DATE_OF_BIRTH)
Error report - Unknown Command
PKG_Generate_GRNo.GenerateGR(TO_NUMBER(:P164_APP_ID,
'9999999'),:APP_USER,:P164_FIRST_NAME,:P164_LAST_NAME,:P164_EMAIL,
:P164_SKYPE_ID,:P164_COUNTRY,:P164_DATE_OF_BIRTH);
Session state protection violation is definitely an Apex error, relating to your page settings. It seems your package is trying to change the state of a read-only page. See this other question.
The item identifier in the error message P164_COURSECOUNT has the same prefix as the parameters you pass to the package (:P164_APP_ID) so presumably they relate to the same page. We know nothing about your application or its architecture, so it's hard to offer concrete advice. Maybe you need to change the page or item settings, maybe you need to change what the package does. Only you can tell the right course of action.
As you didn't post the whole command, a note: you have to enclose it into begin-end block, e.g.
BEGIN
PKG_Generate_GRNo.GenerateGR (TO_NUMBER ( :P164_APP_ID, '9999999'),
:APP_USER,
:P164_FIRST_NAME,
:P164_LAST_NAME,
:P164_EMAIL,
:P164_SKYPE_ID,
:P164_COUNTRY,
:P164_DATE_OF_BIRTH);
END;
/

Difference between oracle call and execute in context of error throwing

I have some procedure written in Oracle. Unfortunately I can't show its code. Somewhere inside it is select where execution crashed because of absence of required data. It looks like this
select value into l_value
from config
where code = upper(p_code);
So when I call this procedure like this (in SqlDeveloper)
execute some_package.some_procedure('CODE');
it throws
Error report -
ORA-01403: no data found
ORA-06512: at "XXXXXXXXXXXXXXXXXXX", line 111
ORA-06512: at "XXXXXXXXXXXXXXXXXXX", line 111
ORA-06512: at line 1
01403. 00000 - "no data found"
*Cause: No data was found from the objects.
*Action: There was no data from the objects which may be due to end of fetch.
But when I call it like this
call some_package.some_procedure('CODE');
it crashes at the same place (as I can suggest from the result, stored in DB), but it does no throw an exception.
some_package.some_procedure('CODE') succeeded.
What happens? And why there is such difference?
NO_DATA_FOUND exception behavior is special. It is handled by default in SQL context but not in PL/SQL. In SQL no data found is not considered as an error, it happens all the time that there is no data that meets certain condition.
CALL is SQL command whereas EXEC is a shortcut for BEGIN <code> END; which is PL/SQL.

customize error message for nhibernate

when deleting an entity in nhibernate i get an exception with this error message:
delete statement conflicted with column reference constraint ..etc
of course the exception is wrapped in long series of exceptions.
the error message is normal, but can i make nhibernate shows more polite error message to the user ??
in another words:
is there any conventions which with, i can customize the exception ??
I'm using Oracle 11g data base.
Yes, you can implement ISQLExceptionConverter to customize the exceptions thrown by NHibernate.
Here's a complete example.