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 ;
Related
i'm trying to translate my oracle trigger to mysql but i'm getting an error. This is my mysql trigger:
delimiter //
CREATE TRIGGER z_asdsdas BEFORE UPDATE ON PRODUCT
FOR EACH ROW
BEGIN
DECLARE v_result INT;
SET v_result = 0;
IF 'BETWEEN' = 'BETWEEN' THEN
IF NEW.PRIJS >= 1 AND NEW.PRIJS <= 10 THEN
SET v_result = 1;
END IF;
ELSE
IF NEW.PRIJS < 1 OR NEW.PRIJS > 10 THEN
SET v_result = 1;
END IF;
END IF;
IF v_result = 0 THEN
signal sqlstate -20000 set msgtext = 'error here...';
END IF;
END //
DELIMITER ;
I gave the mysql trigger static values first to test if it should work. This is my oracle trigger:
CREATE OR REPLACE TRIGGER BRG_<code>_<attribute_table>_TRG
BEFORE DELETE OR INSERT OR UPDATE
ON <attribute_table>
FOR EACH ROW
DECLARE
L_OPER VARCHAR2(3);
L_ERROR_STACK VARCHAR2(4000);
BEGIN
IF INSERTING
THEN
L_OPER := 'INS';
ELSIF UPDATING
THEN
L_OPER := 'UPD';
END IF;
DECLARE
L_PASSED BOOLEAN := TRUE;
BEGIN
IF L_OPER IN ('INS', 'UPD')
THEN
IF '<operator>' = 'BETWEEN' THEN
L_PASSED := :NEW.<attribute_column> <GreaterOrEqual> <range_min> AND :NEW.<attribute_column> <LessOrEqual> <range_max>;
ELSE
L_PASSED := :NEW.<attribute_column> <LessThen> <range_min> OR :NEW.<attribute_column> <GreaterThen> <range_max>;
END IF;
IF NOT L_PASSED
THEN
raise_application_error(-20000, '<error>');
END IF;
END IF;
END;
END;
The compiler is gives red lines at the if-statements and a message: syntax error at line 17. Any idea what i'm doing wrong? Mysql is pretty new for me.
Try to correct the line signalling:
...
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'error here...';
...
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?
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
This is my trigger for show error handling :
DELIMITER //
CREATE TRIGGER NOTA_PENJUALAN_INS BEFORE INSERT ON nota_penjualan FOR EACH ROW
BEGIN
DECLARE VDATE DATETIME;
If (NEW.INVENTORY_OUT_ID IS NOT NULL) then
CALL STATUS_INV_OUT(NEW.INVENTORY_OUT_ID);
SELECT (a.DOCUMENT_DATE) INTO VDATE
FROM inventory_out a
INNER JOIN NOTA_PENJUALAN b ON a.INVENTORY_OUT_ID=b.INVENTORY_OUT_ID
WHERE b.NOTA_PENJUALAN_ID=NEW.NOTA_PENJUALAN_ID;
End IF;
IF(VDATE > NEW.DOCUMENT_DATE) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'sorry can not exceed the date that has been specified';
END IF;
END
//
It displays:
And does not display my ShowMessage.
DELIMITER $$
CREATE TRIGGER insertTrigger BEFORE INSERT ON `agents`
FOR EACH ROW
BEGIN
DECLARE groupID int;
SET groupID = 0;
SET groupID = (SELECT id FROM `groups` WHERE `id` = NEW.group_id);
IF (groupID != 0) THEN
PRINT 'ID is ' + groupID;
ROLLBACK;
END IF;
END$$
DELIMITER ;
The above trigger is created for checking if the foreign key ID exists in the group table.
1) How do I print error messages in mySQL?
2) ROLLBACK Function doesn't work. It gave me the following error message.
"#1422 - Explicit or implicit commit is not allowed in stored function or trigger."
You can use SIGNAL statement to raise an error with custom message from the trigger.
...
IF (groupID != 0) THEN
SET #msg = CONCAT('ID is ', groupID);
SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT = #msg;
END IF;
...
If you want just warning message, then use SQLSTATE code starting with '01...'.