Using variable for error code in MySQL with SQLSTATE - mysql

I'm trying to manage my own errors in MySQL using a procedure and an error message table named "error_messages".
How can I manage to select a specific error message in that table?
I would like to do something like this.
DELIMITER $$
CREATE PROCEDURE throw_error_test (divisor INT)
BEGIN
SET #errorCode = '45123';
IF divisor = 0 THEN
SIGNAL SQLSTATE #errorCode
SET MESSAGE_TEXT = SELECT message
FROM error_messages
WHERE id = #errorCode;
END IF;
END;
Thanks for helping

As documented under SIGNAL Syntax:
The condition_value in a SIGNAL statement indicates the error value to be returned. It can be an SQLSTATE value (a 5-character string literal) or a condition_name that refers to a named condition previously defined with DECLARE ... CONDITION (see Section 13.6.7.1, “DECLARE ... CONDITION Syntax”).
[ deletia ]
Valid simple_value_specification designators can be specified using stored procedure or function parameters, stored program local variables declared with DECLARE, user-defined variables, system variables, or literals.
Therefore, for the MESSAGE_TEXT you could use a user variable—but my preference would be for stored program local variables; however can't specify the SQLSTATE with a variable:
CREATE PROCEDURE throw_error_test (divisor INT)
BEGIN
DECLARE _messageText VARCHAR(128);
IF divisor = 0 THEN
SELECT message
INTO _messageText
FROM error_messages
WHERE id = '45123';
SIGNAL SQLSTATE '45123' SET MESSAGE_TEXT = _messageText
END IF;
END
For the avoidance of doubt, the underscore _ prefixes are purely a convention that I adopt to avoid ambiguity—they are not a necessary part of a local variable name.

Related

IF ELSE statement is producing invalid syntax in MySQL

Not sure why this is returning invalid syntax:
CREATE DEFINER=`root`#`localhost` PROCEDURE `AddColor`(
IN hexvalue varchar(7),
IN notes varchar(50))
BEGIN
IF hexvalue REGEXP '^#[0-9A-F]{6}$' THEN
INSERT INTO data.colors(hexvalue,notes) VALUES (hexvalue,notes);
ELSE
SIGNAL SQLSTATE '400'
SET MESSAGE_TEXT = 'Invalid hex value specified';
END IF;
END
There doesn't seem to be any errors when writing it in.
Based on the following official documentation:
The condition_value in a SIGNAL statement indicates the error value to be returned. It can be an SQLSTATE value (a 5-character string literal) or a condition_name that refers to a named condition previously defined with DECLARE ... CONDITION (see Section 13.6.7.1, “DECLARE ... CONDITION Syntax”).
Try the following syntax and change SIGNAL SQLSTATE value to 04000:
CREATE DEFINER=`root`#`localhost` PROCEDURE `AddColor`(
IN hexvalue varchar(7),
IN notes varchar(50))
BEGIN
IF (hexvalue REGEXP '^#[0-9A-F]{6}$') THEN
INSERT INTO data.colors(`hexvalue`,`notes`) VALUES (hexvalue,notes);
ELSE
SIGNAL SQLSTATE '04000' SET MESSAGE_TEXT = 'Invalid hex value specified';
END IF;
END;

Disallow NULL parameters to stored procedures in MySQL/MariaDB

I can specify that table columns are NOT NULL, but how do I make a stored procedure or function only be compatible with non-null arguments? Adding NOT NULL after the argument name doesn't work.
You would need to validate passed parameter values yourself. If you're using MySQL 5.5 and up you can make use of SIGNAL.
DELIMITER //
CREATE PROCEDURE my_procedure (IN param1 INT)
BEGIN
IF param1 IS NULL THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'NULL is not allowed.';
END IF;
-- do whatever
END//
DELIMITER ;
Here is a SQLFiddle demo

How to parametrize an error in Mysql stored procedure

I am programming the error handling in a MySQL stored procedure using SIGNAL. I want to parametrize the error message to make it more helpful. For example
IF vMyVariable IS NULL THEN
SIGNAL SQLSTATE VALUE '45006'
SET MESSAGE_TEXT = ('Found null variable in iteration' || vId);
END IF;
I tried to use concat() and didn't work.
See https://dev.mysql.com/doc/refman/5.5/en/signal.html
and in particular the section about simple_value_specification
Arbitrary expression can not be used when assigning attributes with SET, as in:
SIGNAL SQLSTATE VALUE '45006' SET MESSAGE_TEXT = <sorry, no arbitrary expressions here>;
However, a local variable is a simple_value_specification, so the following does work:
DECLARE my_message_text VARCHAR(64);
my_message_text := <anything you want>;
SIGNAL SQLSTATE VALUE '45006' SET MESSAGE_TEXT = my_message_text;

Mysql Prevent insert with insert trigger condition not met

I am trying to setup a trigger so an insert does not occur if a condition is not met.
I thought the below was the way to do it but i am not sure
I am getting an error
/* SQL Error (1407): Bad SQLSTATE: '45000 ' */
Can anyone let me know why I am getting this error and the best way for me to prevent an insert if the condition is not met in mysql.
DELIMITER $$
SHOW WARNINGS$$
USE `warrington_central`$$
CREATE TRIGGER before_insert_image_comment_section_check
BEFORE INSERT ON image_comment FOR EACH ROW
BEGIN
DECLARE error_msg varchar(255);
IF New.section != (SELECT id from section where section = "image")
THEN SET error_msg = "Cannot insert a comment into this section as it is the wrong section type";
SIGNAL SQLSTATE '45000 'SET MESSAGE_TEXT = error_msg;
END IF;
END
$$
SHOW WARNINGS$$
SQLSTATE is required to be a a 5 character string unless previously declared using DECLARE ... CONDITION;
SIGNAL SQLSTATE '45000 'SET MESSAGE_TEXT = error_msg;
You're trying to set SQLSTATE to '45000 ' (note the space) which is 6 characters long. Fix the spacing and you should not see the message again (it is also reflected in your error message, but the space is a bit hard to see)

Mysql - How to quit/exit from stored procedure

I have very simple question but i did't get any simple code to exit from SP using Mysql.
Can anyone share with me how to do that?
CREATE PROCEDURE SP_Reporting(IN tablename VARCHAR(20))
BEGIN
IF tablename IS NULL THEN
#Exit this stored procedure here
END IF;
#proceed the code
END;
CREATE PROCEDURE SP_Reporting(IN tablename VARCHAR(20))
proc_label:BEGIN
IF tablename IS NULL THEN
LEAVE proc_label;
END IF;
#proceed the code
END;
If you want an "early exit" for a situation in which there was no error, then use the accepted answer posted by #piotrm. Most typically, however, you will be bailing due to an error condition (especially in a SQL procedure).
As of MySQL v5.5 you can throw an exception. Negating exception handlers, etc. that will achieve the same result, but in a cleaner, more precise manner.
Here's how:
DECLARE CUSTOM_EXCEPTION CONDITION FOR SQLSTATE '45000';
IF <Some Error Condition> THEN
SIGNAL CUSTOM_EXCEPTION
SET MESSAGE_TEXT = 'Your Custom Error Message';
END IF;
Note SQLSTATE '45000' equates to "Unhandled user-defined exception condition". By default, this will produce an error code of 1644 (which has that same meaning). Note that you can throw other condition codes or error codes if you want (plus additional details for exception handling).
For more on this subject, check out:
https://dev.mysql.com/doc/refman/5.5/en/signal.html
How to raise an error within a MySQL function
http://www.databasejournal.com/features/mysql/mysql-error-handling-using-the-signal-and-resignal-statements.html
Addendum
As I'm re-reading this post of mine, I realized I had something additional to add. Prior to MySQL v5.5, there was a way to emulate throwing an exception. It's not the same thing exactly, but this was the analogue: Create an error via calling a procedure which does not exist. Call the procedure by a name which is meaningful in order to get a useful means by which to determine what the problem was. When the error occurs, you'll get to see the line of failure (depending on your execution context).
For example:
CALL AttemptedToInsertSomethingInvalid;
Note that when you create a procedure, there is no validation performed on such things. So while in something like a compiled language, you could never call a function that wasn't there, in a script like this it will simply fail at runtime, which is exactly what is desired in this case!
To handle this situation in a portable way (ie will work on all databases because it doesn’t use MySQL label Kung fu), break the procedure up into logic parts, like this:
CREATE PROCEDURE SP_Reporting(IN tablename VARCHAR(20))
BEGIN
IF tablename IS NOT NULL THEN
CALL SP_Reporting_2(tablename);
END IF;
END;
CREATE PROCEDURE SP_Reporting_2(IN tablename VARCHAR(20))
BEGIN
#proceed with code
END;
This works for me :
CREATE DEFINER=`root`#`%` PROCEDURE `save_package_as_template`( IN package_id int ,
IN bus_fun_temp_id int , OUT o_message VARCHAR (50) ,
OUT o_number INT )
BEGIN
DECLARE v_pkg_name varchar(50) ;
DECLARE v_pkg_temp_id int(10) ;
DECLARE v_workflow_count INT(10);
-- checking if workflow created for package
select count(*) INTO v_workflow_count from workflow w where w.package_id =
package_id ;
this_proc:BEGIN -- this_proc block start here
IF v_workflow_count = 0 THEN
select 'no work flow ' as 'workflow_status' ;
SET o_message ='Work flow is not created for this package.';
SET o_number = -2 ;
LEAVE this_proc;
END IF;
select 'work flow created ' as 'workflow_status' ;
-- To send some message
SET o_message ='SUCCESSFUL';
SET o_number = 1 ;
END ;-- this_proc block end here
END
Why not this:
CREATE PROCEDURE SP_Reporting(IN tablename VARCHAR(20))
BEGIN
IF tablename IS NOT NULL THEN
#proceed the code
END IF;
# Do nothing otherwise
END;
MainLabel:BEGIN
IF (<condition>) IS NOT NULL THEN
LEAVE MainLabel;
END IF;
....code
i.e.
IF (#skipMe) IS NOT NULL THEN /* #skipMe returns Null if never set or set to NULL */
LEAVE MainLabel;
END IF;
I think this solution is handy if you can test the value of the error field later. This is also applicable by creating a temporary table and returning a list of errors.
DROP PROCEDURE IF EXISTS $procName;
DELIMITER //
CREATE PROCEDURE $procName($params)
BEGIN
DECLARE error INT DEFAULT 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET error = 1;
SELECT
$fields
FROM $tables
WHERE $where
ORDER BY $sorting LIMIT 1
INTO $vars;
IF error = 0 THEN
SELECT $vars;
ELSE
SELECT 1 AS error;
SET #error = 0;
END IF;
END//
CALL $procName($effp);