How do i output an SQL Error in MySQL - 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.

Related

Exception handling in amazon redshift

Need to know in Redshift database if there is a option to capture error code or error message in the exception block similar to error code (SQLCODE) and error description(SQLERRM) in Oracle. Requirement is to capture the error code or error message occuring in procedure and insert it into error logging table. Kindly suggest.
The question is quiet old. I hope you managed to find what you were looking for. Im adding up this answer so this answer can help other people.
In an Amazon Redshift stored procedure, the only supported handler_statement is RAISE. Any error encountered during the execution automatically ends the entire stored procedure call and rolls back the transaction. This occurs because subtransactions are not supported.
SYNTAX :
[ <<label>> ]
[ DECLARE
declarations ]
BEGIN
statements
EXCEPTION
WHEN OTHERS THEN
handler_statements
END;
For detail understanding, visit its Documentation.

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.

Logging with MYSQL Handlers

I'm hoping this isn't just duplicating this question.
I have read the documentation but I don't think I fully understand how to use this properly yet.
I would like to catch errors thrown in my stored procedures and log those errors in a table. This is mostly for experimental purposes. I am wondering is there a way to catch any error and then log the code and error to a table.
So far it looks like I have to declare a different handler for each error. Is this correct or is there a way to catch all errors and get the code and message.
For example in each stored procedure I'm declaring a couple of handlers
DECLARE EXIT HANDLER FOR 1062
BEGIN
SELECT 'Attempt to create a duplicate entry occurred';
END;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
SELECT 'Unexpected Error Ocurred';
END;
Instead of
SELECT 'custom message';
I want to do
INSERT INTO errorLogs(code, message);
Is this possible without declaring a load of handlers and adding each code manually?
Really appreciate any help pointing me in the right direction.
It looks like this is not really possible until I can use an updated version of MYSQL with DIAGNOSTICS. I found this question which is basically the same as mine. Ah well.

MySQL Error 1415

I'm currently coding somewhat on MySQL and got a FUNCTION.
For me the code looks fine, but MySQL throws a
Error Code: 1415
Not allowed to return a result set from a function
Error.
Function:
Cick here, pastebin.com
As i said, i dont see any syntax error in it and simply cant understand why its throwing an Error. Only on execution though, creates without any problems.
I googled already, and haven't found anything helping my on this, so i hope you can.
Thanks in advice.
I replaced all the other functions referenced in your new function with ROUND(RAND()) and it ran fine; Could it be that one of those functions might be trying to return a result set?

Problems calling MySQL stored procedures from C API

I have a problem calling a stored procedure on my MySQL server using the C API.
I use mysql_query(&handle,"CALL myprocedure") but the function fails (returns 1) and error lookup gives
the following message "Procedure myprocedure can't return a result set in the given context."
I even tried to use mysql_real_query insted, but no better.
I've seen a few topics about this bug, but only PHP related. So there seems to be the same problem for C programs too.
The weird thing is that my stored procedure is not even supposed to return any result set. It just works with data in tables, doesn't really return anything.
Thanks for any advices.
Refer to functions:
mysql_set_server_option() &
mysql_real_connect()
here.
Multiple statements are only enabled(temporarily) using the MYSQL_OPTION_MULTI_STATEMENTS_ON and _OFF arguments
to mysql_set_server_option().
The problem here is that CLIENT_MULTI_STATEMENTS in
mysql_real_connects()
implicitly enables CLIENT_MULTI_RESULTS, too, but
MYSQL_OPTION_MULTI_STATEMENTS_ON only enables multiple statements,
not multiple results.
So add CLIENT_MULTI_STATEMENTS on connect and try again.
Did you try to call this procedure from 'mysql' command line client?
Are you able to call (another) empty procedure to test if problem is related to the procedure?