I am trying to create a trigger for my table named quailmiles. When the total miles hits a certain number I want the persons status (which is a attribute in that same table) to change. For some reason my code is not working.
The error currently reads:
#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 9.
delimiter $$
CREATE TRIGGER status_update
AFTER UPDATE ON quailmiles
FOR EACH ROW BEGIN
IF (total_miles > 2000) THEN
UPDATE quailmiles SET status = 'gold';
ELSE IF (total_miles > 4000) THEN
UPDATE quailmiles SET status = 'platinum';
END IF;
END $$
delimiter;
Your logic is backwards. You need to test against 4000 before 2000:
delimiter $$
CREATE TRIGGER status_update
BEFORE UPDATE ON qualmiles
FOR EACH ROW
BEGIN
IF (NEW.total_miles > 4000) THEN
SET NEW.status = 'platinum';
ELSEIF (NEW.total_miles > 2000) THEN
SET NEW.status = 'gold';
END IF;
END $$
delimiter;
I also changed the syntax to be a "before" update trigger. Also, I made the name "qualmiles". "Quailmiles" sounds funny.
I think you want ELSEIF rather than ELSE IF. (Remove the space.)
The error is being returned because that second IF isn't being ended with an END IF.
Also, you don't really want to issue an UPDATE against the same table. You'd rather this be handled in a BEFORE UPDATE trigger, and set the value of the column.
e.g.
CREATE TRIGGER status_update
BEFORE UPDATE ON quailmiles
FOR EACH ROW
BEGIN
IF (NEW.total_miles > 4000) THEN
SET NEW.status = 'platinum';
ELSIF (NEW.total_miles > 2000) THEN
SET NEW.status = 'gold';
END IF;
END$$
Related
I'm trying to create this trigger but i'm getting
[Err] 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 '; CREATE TRIGGER ca_passwd_trigger BEFORE UPDATE ON users
FOR EACH ROW BEGIN ' at line 1
delimiter $$
DROP TRIGGER IF EXISTS ca_passwd_trigger ;
$$
CREATE TRIGGER ca_passwd_trigger BEFORE UPDATE ON users
FOR EACH ROW
BEGIN
IF ((NEW.passwd <=> OLD.passwd) = 0) THEN
SET NEW.passwd_modified_at = NOW();
END IF;
END;$$
delimiter ;
In your query you added the query terminator ; with the delimiter $$ in two places. The below query is having proper query terminators and delimiters.
DELIMITER $$
DROP TRIGGER IF EXISTS ca_passwd_trigger; -- removed the delimiter $$ after the terminator ;
CREATE TRIGGER ca_passwd_trigger BEFORE UPDATE ON users
FOR EACH ROW
BEGIN
IF ((NEW.passwd <=> OLD.passwd) = 0) THEN
SET NEW.passwd_modified_at = NOW();
END IF;
END$$ -- removed the terminator ; before the delimiter $$
DELIMITER ;
I try to make loop in MySQL, but it always shows me an error.
Is it correct code or did I make a mistake?
DROP PROCEDURE IF EXISTS proc_loop_test;
CREATE PROCEDURE proc_loop_test()
BEGIN
DECLARE int_val INT DEFAULT 0;
test_loop : LOOP
IF (int_val = 10) THEN
LEAVE test_loop;
END IF;
SET int_val = int_val +1;
SELECT int_val;
END LOOP;
END;
#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 3
The problem is that the default delimiter is ; and stops the execution/parsing of the procedure code. Change the delimiter before you create the procedure and set it back to ; after the procedure code like this:
DROP PROCEDURE IF EXISTS proc_loop_test;
DELIMITER #
CREATE PROCEDURE proc_loop_test()
BEGIN
...
END #
DELIMITER ;
This might be a noob question, but I tried googling about this issue and I haven't found any solution for it (I think). Anyway, I need to compare a date in a row from a mysql table to the current date and update the row depending on the results. I made a stored procedure that will be called by an event every x minutes. Here is my code for the sp:
delimiter //
CREATE PROCEDURE checkPromoValidity()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE id INT;
DECLARE lvn_status INT;
DECLARE lvn_validUntil DATE;
DECLARE cur1 CURSOR FOR SELECT promos.PROMO_NUMBER,promos.VALID_UNTIL,promos.status FROM MDCH_NEW.promos;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open cur1;
check_loop: LOOP
fetch cur1 into id,lvn_validUntil,lvn_status;
IF done THEN
LEAVE check_loop;
END IF;
IF (lvn_validUntil > CURDATE()) THEN
update mdch_new.promos set MDCH_NEW.PROMOS.status = 0 where mdch_new.promos.PROMO_NUMBER = id;
END IF;
end LOOP;
close cur1;
END //
MDCH_NEW = database name
PROMOS= table name
EDIT: I CHANGED my delimiter to // just to see
edit2: added space after the final word, still getting the error below.. :(
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 23
delimiter $$
create procedure return7 ()
BEGIN
select 7;
END
$$
call return7();
you are free to re-use this code
Dummy question: Have the next code in MySQL, but when run it in HeidiSQL, show a sintaxis error near '// CREATE TRIGGER'. I'm not sure if that happen because the trigger doesn't exists. This is the code:
DROP TRIGGER IF EXISTS oportunidades_movimiento_entregado //
CREATE TRIGGER oportunidades_movimiento_entregado;
AFTER INSERT ON historial_entregado
FOR EACH ROW BEGIN
UPDATE oportunidades
SET oportunidades.fechaModificado = NEW.fecha_creacion
WHERE NEW.oportunidad_id = oportunidades.id;
END//
UPDATE
DELIMITER $$
DROP TRIGGER IF EXISTS oportunidades_movimiento_entregado $$
CREATE TRIGGER oportunidades_movimiento_entregado
AFTER INSERT ON historial_entregado
FOR EACH ROW BEGIN
UPDATE oportunidades
SET oportunidades.fechaModificado = NEW.fecha_creacion
WHERE NEW.oportunidad_id = oportunidades.id;
END; $$
DELIMITER //
This works, but now my doubt is... All the code (the DROP and the CREATE) will run everytime, or just the CREATE? I need this because I'm working with 2 tables, in one of them I insert/update the data, after with a trigger insert some of the columns in another table to keep a historial, but when I make a INSERT or UPDATE query in the first table, MySQL display the error #1442
Can't update table oportunidades in stored/function trigger because it's already used by statement which invoked this stored function/trigger.
To get it working, set the delimiter first and reset it after the CREATE TRIGGER statement.
Remove the semicolon at the end of the CREATE TRIGGER line.
This should work:
DELIMITER //
DROP TRIGGER IF EXISTS oportunidades_movimiento_entregado //
CREATE TRIGGER oportunidades_movimiento_entregado -- no trailing semicolon here
AFTER INSERT ON historial_entregado
FOR EACH ROW BEGIN
UPDATE oportunidades
SET oportunidades.fechaModificado = NEW.fecha_creacion
WHERE NEW.oportunidad_id = oportunidades.id;
END//
DELIMITER ;
whats wrong with my syntax?
CREATE
TRIGGER db_dhruniversity.trigger1
AFTER INSERT
ON jos_dhruprofile
FOR EACH ROW
BEGIN
UPDATE jos_users
SET jos_users.department = jos_dhruprofile.department
WHERE jos_users.id = jos_dhruprofile.uid
END
The syntax should be as follows:
DELIMITER $$ /* if you're not using an editor, you must change the delimiter*/
CREATE
TRIGGER ai_jos_dhruprofile_each
AFTER INSERT
ON jos_dhruprofile
FOR EACH ROW
BEGIN
UPDATE jos_users
SET jos_users.department = NEW.department
WHERE jos_users.id = NEW.uid; /*<<<--- ; after every stament */
END $$ /* changed delimiter after the end */
DELIMITER ; /*make sure you set the delimiter back to the default*/
Note on the naming scheme for triggers
I'd recommend naming your trigger ai (meaning after insert) so you know when it fires on which table, rather than a meaningless name like: db_dhruniversity.trigger1.
I always use [a/b]+[d/i/u]_tablename_each as the triggername, that way I always know when the triggers fires (before/after) for which event (insert/delete/update) and on which table.
It's also good practise to document that the trigger fires on each row, hence the each on the end of the trigger name.
Note that MySQL does not support triggers that fire once per statement yet (But that might change in future).
There are no delimiters in it:
DELIMITER ||
CREATE
TRIGGER db_dhruniversity.trigger1
AFTER INSERT
ON jos_dhruprofile
FOR EACH ROW
BEGIN
UPDATE jos_users
SET jos_users.department = NEW.department
WHERE jos_users.id = NEW.uid;
END ||
DELIMITER;