IF ELSE statement is producing invalid syntax in MySQL - 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;

Related

Mysql trigger creation bad syntax

I'm trying to create a SQL Trigger, but i receive always an error message, i want to check on update and insert operation if value called status is not higher than 1 and not lower than 0.
CREATE TRIGGER tr_ins_cert_login
BEFORE UPDATE ON cert_login
FOR EACH ROW
BEGIN
DECLARE errorMessage VARCHAR(255);
SET errorMessage('Invalid Status Value.');
IF new.status <> 0 AND new.status <> 1 THEN
SET MESSAGE_TEXT = errorMessage;
END IF;
END
When i try to run the commands it returns error.
I'm using MySql.
The error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5
Any hint?
Message_text should be part of signal sqlstate following syntaxes on my box
drop trigger if exists t;
delimiter $$
CREATE TRIGGER t
BEFORE UPDATE ON cert_login
FOR EACH ROW
BEGIN
DECLARE errorMessage VARCHAR(255);
SET errorMessage = ('Invalid Status Value.');
IF new.status <> 0 AND new.status <> 1 THEN
SIGNAL SQLSTATE '01000'
SET MESSAGE_TEXT = errorMessage;
END IF;
END $$
delimiter ;
and https://www.db-fiddle.com/f/913nweKrHqXL5SViuuqAZE/0
ps your first set statement is invalid you missed the =

MYSQL IF do nothing ELSE

I have a table users. When someone one to a insert new record which contains in name or in surname character diffrent from a-z,A-Z or space it will be a mistake and error occur. The code fails because after first THEN should be something, but i dont know what to put insidee. Because I only want to execute insert when if statement is true.
delimiter //
CREATE TRIGGER check_name_surname
BEFORE INSERT ON users
FOR EACH ROW
BEGIN
IF NEW.name REGEXP '^[a-zA-Z' '.]+$' and NEW.surname REGEXP '^[a-zA-Z' '.]+$' then
ELSE
SIGNAL SQLSTATE VALUE '45000' SET MESSAGE_TEXT = 'Incorrect name and surname';
END IF;
END;//
delimiter;
When you have nothing to put in your IF statement then you have to 'negate' the else condition and put it in the if.
So here you will throw an error IF name is not alphanumeric OR surname is not alphanumeric.
IF NEW.name NOT REGEXP '^[a-zA-Z' '.]+$' OR NEW.surname NOT REGEXP '^[a-zA-Z' '.]+$' THEN
SIGNAL SQLSTATE VALUE '45000' SET MESSAGE_TEXT = 'Incorrect name and surname';
END IF;
Do not forget that the AND operator become OR.

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

Using variable for error code in MySQL with SQLSTATE

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.

mysql weird trigger errors

I'm trying to set up a trigger (run from a separate .sql file) to prevent insertion like this:
create trigger insert_formulas before insert on formulas
for each row
if ((new.formula_id_1 is not null) and (new.semantic_id_1 is not null))
or ((new.formula_id_2 is not null) and (new.semantic_id_2 is not null))
then
signal sqlstate '45100';
set #message_text = 'A formula cannot be a definition at the same time';
end if;
It produces an error 1064 near '' at line 6 and near 'end if' at line 1. What have I done wrong?
Thanks in advance.
Does the 9.4. User-Defined Variables #message_text is different from MESSAGE_TEXT from the definition of 13.6.7.5. SIGNAL Syntax?
The following code creates the trigger correctly:
DELIMITER $$
CREATE TRIGGER `insert_formulas` BEFORE INSERT ON `formulas`
FOR EACH ROW
BEGIN
IF((NEW.`formula_id_1` IS NOT NULL AND NEW.`semantic_id_1` IS NOT NULL) OR
(NEW.`formula_id_2` IS NOT NULL AND NEW.`semantic_id_2` IS NOT NULL)) THEN
SIGNAL SQLSTATE '45100'
SET MESSAGE_TEXT = 'A formula cannot be a definition at the same time';
END IF;
END$$
DELIMITER ;
Here SQL Fiddle.