Formatting error message in MySQL error - mysql

I am trying to figure out how to throw an error with formatted error message in MySQL (5.7).
// SOME PROCEDURE
begin
declare something INT;
start transaction;
call getsomething(something); // sets something to data
if something is NULL then
rollback;
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Something %d not found!', MYSQL_ERRNO = 1001;
end if;
commit;
end
How can I use "something" variable when creating error message?

// SOME PROCEDURE
begin
declare something INT;
declare error_msg VARCHAR(255);
start transaction;
call getsomething(something); // sets something to data
if something is NULL then
rollback;
set error_msg = CONCAT('Something not found!', something);
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = error_msg, MYSQL_ERRNO = 1001;
end if;
commit;
end
Following #Samis comment and retrying using CONCAT - above works.

What about CONCAT() and local variable to store the message?
SET MESSAGE_TEXT = CONCAT(something, 'not found!');
working example

Related

Cannot call Stored procedure Error code 1305

The database is about exam scheduling and this is the main stored procedure which is about scheduling courses on a given date.
I created the procedure but when I try to call it says the procedure is not found.
I've looked through the code but can't find syntax errors, the procedure itself does not have compilation errors, (but that may be because I have to use delimiters?)
DELIMITER $$
CREATE PROCEDURE schedule_course(IN in_code CHAR(3), IN in_date DATE)
BEGIN
DECLARE complete BOOLEAN DEFAULT FALSE;
DECLARE module_code CHAR(3);
DECLARE module_c CURSOR FOR
SELECT course_code FROM module WHERE course_code = in_code;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET complete = TRUE;
IF (DAYOFWEEK(in_date) = 6 OR 7) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE TEXT = 'Cannot schedule start date on a weekend'
END IF;
OPEN module_c;
mainloop : LOOP
FETCH NEXT FROM module_c INTO module_code;
IF complete THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE TEXT = 'Something something'
END IF;
INSERT INTO SESSION (`code`, `date`, room)
VALUES
(module_code, in_date, NULL)
LEAVE mainloop;
END LOOP;
DELIMITER ;
CALL schedule_course(WSD, CURDATE())
Error Code: 1305. PROCEDURE cameron.schedule_course does not exist
Mysql 8 show a lot of errors.
I corrected them, the logic is sound but of course you have to check it,
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE schedule_course(IN in_code CHAR(3), IN in_date DATE)
BEGIN
DECLARE complete BOOLEAN DEFAULT FALSE;
DECLARE module_code CHAR(3);
DECLARE module_c CURSOR FOR
SELECT course_code FROM module WHERE course_code = in_code;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET complete = TRUE;
IF (DAYOFWEEK(in_date) = 6 OR DAYOFWEEK(in_date) = 7) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Cannot schedule start date on a weekend';
END IF;
OPEN module_c;
mainloop : LOOP
FETCH NEXT FROM module_c INTO module_code;
IF complete THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Something something';
END IF;
INSERT INTO SESSION (`code`, `date`, room)
VALUES
(module_code, in_date, NULL);
LEAVE mainloop;
END LOOP;
CLOSE module_c;
END;
DELIMITER ;
seems you have not an end (END $$)
DELIMITER $$
CREATE PROCEDURE schedule_course(IN in_code CHAR(3), IN in_date DATE)
BEGIN
DECLARE complete BOOLEAN DEFAULT FALSE;
DECLARE module_code CHAR(3);
DECLARE module_c CURSOR FOR
SELECT course_code FROM module WHERE course_code = in_code;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET complete = TRUE;
IF (DAYOFWEEK(in_date) = 6 OR 7) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE TEXT = 'Cannot schedule start date on a weekend'
END IF;
OPEN module_c;
mainloop : LOOP
FETCH NEXT FROM module_c INTO module_code;
IF complete THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE TEXT = 'Something something'
END IF;
INSERT INTO SESSION (`code`, `date`, room)
VALUES
(module_code, in_date, NULL)
LEAVE mainloop;
END LOOP;
END $$
DELIMITER ;

Is is possible to set out parameter while signal an error for MySQL stored procedure?

I want my stroed procedure both set out paramters to NULL and error message if excution failed. Here is my approach:
CREATE PROCEDURE p(OUT outp INT)
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
SET outp = NULL;
RESIGNAL; -- without resignal output is set.
END;
START TRANSACTION;
SELECT id INTO outp FROM some_table WHERE some_condition;
IF outp IS NULL THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'outp is null';
ELSE
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'outp not null';
END IF;
COMMIT;
END
The test code is:
SET #id = 987;
CALL p(#id);
SELECT #id;
With resignal statement on line 7, the error message pops but #id remains 987.
Without reginal statement on line 7, #id is set to NULL but no error messages.
Is it possible to do both?

The System Variable "MESSAGE_TEXT" is not working with SIGNAL statement in Mysql 5.6?

My code is:i am using mysql 5.6 and workbench 6.3CE
CREATE DEFINER=`root`#`windows7test-pc` PROCEDURE `p`(divisor INT)
BEGIN
Declare MESSAGE_TEXT varchar(200);
IF divisor = 0 THEN
BEGIN
DECLARE my_error CONDITION FOR SQLSTATE '45000';
SIGNAL my_error;
set MESSAGE_TEXT='error occured in if block';
END;
END IF;
END
when i apply this stored procedure then i get no error but when i run this by calling call p(0) statement then i get only system error message which is Error Code: 1644
Unhandled user-defined exception condition.but my question is that why the mysql server not generate my error message whic is MESSAGE_TEXT='error occured in if block'; ?
Use SIGNAL SQLSTATE:
CREATE PROCEDURE `p`(divisor INT)
BEGIN
Declare MESSAGE_TEXT varchar(200);
IF divisor = 0 THEN
BEGIN
SIGNAL SQLSTATE '45000'
set MESSAGE_TEXT='error occured in if block';
END;
END IF;
END
CALL `p`(0)
-- error occured in if block
SqlFiddleDemo

MySQL : Trigger Syntax not correct

I'm trying to write a trigger for a table named classes_tbl where I basically want to check before update if the participants are at the maximum, if true - do not allow update, and raise error message.
As well as check if participants = 0 in case the the update table tries to decrease the number, it needs not to be possible to decrease below 0 - and again raise an error message in this case.
I'm not 100% accustomed to MySQL so I'm not sure what is missing or wrong. It would be nice If someone could give me a hint.
Here is my Create Trigger syntax:
DELIMITER //
CREATE TRIGGER MaxBelegt_TRGGR
BEFORE UPDATE ON classes_tbl
FOR EACH ROW
BEGIN
DECLARE
n_count NUMBER(3);
msg varchar(255);
SELECT maxParticipants
INTO n_count
FROM rooms_tbl
WHERE rid = :NEW.rid;
IF :OLD.participants = n_count THEN
set msg = concat('MyTriggerError: MaxParticipants for Room exceeded. On MaxBelegt_TRGGR -> belegtAnz : ', cast(OLD.participants as char));
signal sqlstate '45000' set message_text = msg;
END IF;
IF :NEW.participants < 0 THEN
set msg = concat('MyTriggerError: Participant number already 0. On MaxBelegt_TRGGR -> belegtAnz : ', cast(OLD.participants as char));
signal sqlstate '45000' set message_text = msg;
END IF;
END
//
DELIMITER;
Thank You!
BTW: I'm using mysql version : 5.6.24 .. just in case

MySQL trigger definition - 1064 error

My proposed trigger:
#START TRIGGER
delimiter //
DECLARE msg VARCHAR(255);
CREATE TRIGGER passStandard_check BEFORE INSERT ON Module
FOR EACH ROW
BEGIN
IF NEW.passStandard < 0 || NEW.passStandard > 1 THEN
set msg = concat('Trigger Error: Pass Standard: ', cast(NEW.passStandard as char));
signal sqlstate '45000' set message_text = msg;
END
//
delimiter ;
#END TRIGGER
But I get the following error:
Error Code: 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 'DECLARE msg VARCHAR(255); CREATE TRIGGER passStandard_check BEFORE INSERT ON Mod' at line 1
Adding in END IF does not make any difference.
passStandard is set to an INT NOT NULL.
Edit
I've moved the DECLARE statement after the BEGIN:
#START TRIGGER
delimiter //
CREATE TRIGGER passStandard_check BEFORE INSERT ON Module
FOR EACH ROW
BEGIN
DECLARE msg VARCHAR(255);
IF NEW.passStandard < 0 || NEW.passStandard > 1 THEN
set msg = concat('Trigger Error: Pass Standard: ', cast(NEW.passStandard as char));
signal sqlstate '45000' set message_text = msg;
END IF;
END
//
delimiter ;
#END TRIGGER
But I still get this error:
Error Code: 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 'sqlstate '45000' set message_text = msg; END IF; END' at line 7
You need to declare your variable "msg" inside your procedure and use END IF
#START TRIGGER
delimiter //
CREATE TRIGGER passStandard_check BEFORE INSERT ON Module
FOR EACH ROW
BEGIN
DECLARE msg VARCHAR(255); /* << PUT THIS HERE */
IF NEW.passStandard < 0 || NEW.passStandard > 1 THEN
set msg = concat('Trigger Error: Pass Standard: ', cast(NEW.passStandard as char));
signal sqlstate '45000' set message_text = msg;
END IF; /* << YOU WILL NEED THIS TOO (DONT FORGET THE SEMICOLON :D) */
END//
delimiter ;
#END TRIGGER
I noticed that I was getting MySQL warnings when adding triggers with IF/ELSE logic in them if I wasn't adding parentheses to the IF statements. Not sure why, but I added the brackets and the warnings went away. Try something like:
IF ((NEW.passStandard < 0) || (NEW.passStandard > 1)) THEN
set msg = concat('Trigger Error: Pass Standard: ', cast(NEW.passStandard as char));
signal sqlstate '45000' set message_text = msg;
END IF;
Try breaking it up to several queries. Or run it in phpmyadmin which handles semi colon separated multi queries.
Might be this:
DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.
http://dev.mysql.com/doc/refman/5.0/en/declare.html
i got it working. pls find below the working code.
delimiter //
CREATE TRIGGER test_check1 BEFORE INSERT ON employee
FOR EACH ROW
BEGIN
DECLARE msg VARCHAR(255);
IF (NEW.sync_id = '0') THEN
SET NEW.name = 'Collect Money';
signal sqlstate '45000' set message_text = msg;
END IF;
END//
delimiter ;