I am having a problem using CONCAT() in a MySQL procedure.
I am using MySQL version: 8.0.22
From the command line, CONCAT works for the following two scenarios. So, it appears to me that I should be able to use one of these formats in a procedure.
root@localhost [mdy_exp_constraints]> SET @primary_color_name = 'yellow';
Query OK, 0 rows affected (0.00 sec)
root@localhost [mdy_exp_constraints]> SELECT CONCAT('Data too short for ', @primary_color_name, ' column.');
+----------------------------------------------------------------+
| CONCAT('Data too short for ', @primary_color_name, ' column.') |
+----------------------------------------------------------------+
| Data too short for yellow column. |
+----------------------------------------------------------------+
1 row in set (0.00 sec)
root@localhost [mdy_exp_constraints]> SET @woof = CONCAT('Data too short for ', @primary_color_name, ' column.');
Query OK, 0 rows affected (0.00 sec)
root@localhost [mdy_exp_constraints]> SELECT @woof;
+-----------------------------------+
| @woof |
+-----------------------------------+
| Data too short for yellow column. |
+-----------------------------------+
1 row in set (0.00 sec)
This is the data that I use to test my procedure
CALL mdy_exp_constraints.checkColorsForTooFewChars('red', 'ff');
Query OK, 0 rows affected (0.00 sec)
IN primary_color_value CHAR(2)
. This error is handled by the MySQl server.CALL mdy_exp_constraints.checkColorsForTooFewChars('green', 'fff');
ERROR 1406 (22001): Data too long for column 'primary_color_value' at row 1
CALL mdy_exp_constraints.checkColorsForTooFewChars('blue', 'a');
ERROR 1644 (45000): Data too short for column.
Below, I define my procedure.
CREATE SCHEMA mdy_exp_constraints
DEFAULT CHARACTER SET utf8mb4
COLLATE utf8mb4_0900_ai_ci;
USE mdy_exp_constraints;
DROP PROCEDURE IF EXISTS mdy_exp_constraints.checkColorsForTooFewChars;
SHOW COUNT(*) WARNINGS;
SHOW WARNINGS;
# -- SHOW all PROCEDUREs for the specified database.
SHOW
PROCEDURE STATUS
WHERE
db = 'mdy_exp_constraints'\G
DELIMITER $
CREATE PROCEDURE checkColorsForTooFewChars(
IN primary_color_name VARCHAR(32),
IN primary_color_value CHAR(2)
)
BEGIN
DECLARE required_str_length TINYINT;
SET required_str_length = 2;
IF CHAR_LENGTH(primary_color_value) < required_str_length THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = CONCAT('Data too short for ', primary_color_name, ' column.');
-- SET MESSAGE_TEXT = 'Data too short for column.';
END IF;
END $
DELIMITER ;
And this is the error that I am receiving.
ERROR 1064 (42000): 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 '('Data too short for ', primary_color_name, ' column.');
END IF' at line 11
If I comment out the line that contains the CONCAT() and un-comment the line not containing the CONCAT(), this procedure works when running it against the test data...except, that it will not report the primary_color_name
, which is what I would like for it to do.
Thanks,
Mike
The error occurs because SET MESSAGE_TEXT
does not accept expressions.
DELIMITER $
CREATE PROCEDURE checkColorsForTooFewChars(
IN primary_color_name VARCHAR(32),
IN primary_color_value CHAR(2)
)
BEGIN
DECLARE required_str_length TINYINT;
SET required_str_length = 2;
IF CHAR_LENGTH(primary_color_value) < required_str_length THEN
SET @errorMsg = CONCAT('Data too short for ', primary_color_name, ' column.');
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = @errorMsg;
END IF;
END $
DELIMITER ;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.