'Return'/Abort mySql stored procedure - mysql

I want to abort/quit/return from a stored procedure when a condition is not met.
I'm not sure how to do it. Googling didn't help me much because:
The version of MySql on our servers is 5.1 (<5.5 and hence no signal sqlstate).
I don't want to put everything in if..then..else statements (there will be multiple levels of nesting in my case)
I don't want to call an_invalid_procedure to raise an exception.
Are there alternatives?

Something wrong with....
...
BEGIN
<do stuff>
IF (<your condition>) THEN
<do more stuff>
END IF;
END$$

What I do in such cases is to:
Create table called errors with one column containing textual error messages and UNIQUE index on it. I fill the table with errors I want to support.
When I need specific error to raised, I insert the error message into errors table. Since it's already there, I am violating a UNIQUE key so error is raised. In my application I can watch for MySQL error #1062 (that's the code for 'Duplicate value for key...' error and use regexp to extract my error message to show it to user/log it/whatever;

Related

Is It Possible for a MYSQL Trigger to Return an Error Message if the Trigger Fails for Any Reason?

I am trying to create an after insert trigger in MySQL that writes the message "Insert Failed" to an error table called tbl_error if the trigger fails for any reason.
To solve this problem, I have attempted to use a combination of handlers and signal statements. None have worked.
DELIMITER $$
CREATE TRIGGER trig_statesout_afterinsert AFTER INSERT ON states
FOR EACH ROW
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SIGNAL SQLSTATE VALUE '99999'
SET MESSAGE_TEXT = 'Insert Failed';
INSERT INTO tbl_error(ErrorMessage) Values ('Insert Failed');
END;
INSERT INTO statesout (states_id, state, cases, lastupdate)
SELECT s.states_id, s.state, s.cases, current_timestamp()
FROM states as s
WHERE s.states_id = NEW.states_id;
END $$
I attempted to return information from a diagnostic statement, but that didn't work either.
Because the documentation states that
For SQLEXCEPTION conditions, the stored program terminates at the
statement that raised the condition, as if there were an EXIT handler.
If the program was called by another stored program, the calling
program handles the condition using the handler selection rules
applied to its own handlers.
I split the update logic for the trigger into a separate stored procedure in case the issue was that the SQLEXCEPTION conditions cannot be in the calling object. It didn't work.
Some SO solutions suggest using a "trick" to call a nonexistent table, but the documentation says this is not necessary due to the SIGNAL statement:
Without SIGNAL, it is necessary to resort to workarounds such as
deliberately referring to a nonexistent table to cause a routine to
return an error.
In any case, those solutions only throw errors based on a specific condition, not for any condition.
What am I doing wrong? Is it impossible for a MYSQL trigger to return an error message if the trigger fails for any reason?
It's not possible to make any change to data if the trigger activates a SQLException.
If the trigger experiences any error, whether handled or not handled, then the INSERT that spawned the trigger is undone, as well as any SQL run by the trigger, and even any SQL run by triggers spawned by actions your trigger executes.
This is related to the idea of atomicity — either the whole action must succeed, or else no part of the action is saved. It's all or nothing.
If you want to log the error, you'll have to check for the error in your application, and then log the error as a separate action. You can't do it in the trigger body.

Why error occurs when creating the trigger?

I am trying to add a trigger through phpmyadmin 4.4
I get the error:
Why is this happening? How to declare a variable?
Start with BEGIN, end with END. Put the various statements between them.
Notice in the error how it is alread providing FOR EACH ..., but it is depending on you to do the rest. See http://dev.mysql.com/doc/refman/5.7/en/triggers.html (and other pages) for the complete syntax that phpmyadmin is "helping" you generate.

Testing for MySQL errors

Whenever MySql encounters an error, it stops processing, even if there are more statements following. I need it to report errors but continue processing the remaining statements. I read in http://dev.mysql.com/doc/mysqltest/2.0/en/writing-tests-expecting-errors.html that you can use --error but MySql just says I have a syntax error.
For example, this stored procedure which adds a name to a unique column should cause a 1062 error...
...
call AddName('Joe');
--error 1062
call AddName('Joe');
...
...but MySql just points out the line number and says syntax error near '--error 1062 call AddName('Joe');'
It's also worth noting that comments starting with "--" don't seem to register as comments. I imagine that might be related.
Thanks in advance.

How do i output an SQL Error in MySQL

I've declared an handler which will handle SQLEXCEPTION, in the clean up code i had it do SELECT 'My Handle';, i ran a script that fails because of a primary key violation and it worked cause i got my output.
the problem with DECLARE ... HANDLE FOR SQLEXCEPTION is that when there is an error it will run though it's doesn't say what error triggered it, so i want to output the error
How do i output the SQL Error using a MySQL Query, i don't care if i can only output the error code/id i just need something to output giving me an indication on what the error is so i can fix the problem
EDIT: in case if it's not obvious, this code is in an SQL Procedure
There is nothing in the DECLARE HANDLER documentation on this functionality. You could handle the errors MySQL returns to your application and print or log them that way.
I actually haven't used any handles, but i think you can make your code into a stored procedure and then run the stored procedure!! You might find MySQL accepts what you are trying to do then.

DB2 Exception Handling

The problem that I am facing is primarily on Exception Handling! When an exception occurs I want to put that data in another log table with the error message. However, in DB2 I am not able to figure out a way to retrieve the corresponding error message for the raised SQLSTATE.
PS: I have a stored procedure for this migration and I am not using any other language to call it.
Though, I have already queried about this and infact I got some valuable pointers.
Refer: DB2 Exception handling
However, if I use the basic SQLERRM function then for a basic 23502 I get the following message:
"SQLSTATE 23502: An insert or update value is null, but the column cannot contain null values."
Whereas what I really want is the Column name that threw this error, appended to this message! Is there any way, the DB2 could give me a complete error, with the column name the error was raised in?
Thanks in advance ;-)...
Harveer
You need to retrieve all of the tokens from the SQLCA when you encounter such an error. The tokens will contain either the table name and column name, or the numeric table id and column position within the table, starting with zero. The SQLERRM function takes in those tokens and uses them to reconstruct an error message that is as detailed as what you see from the command line.