Trigger syntax error - mysql

My first time using triggers. Can anyone please explain why this trigger won't work? The error I'm getting is inconclusive (error near '' at line 5)
create trigger queue after update on downloads
for each row
begin
if NEW.completed = 1 then
insert into s_queue ( website_id ) values ( NEW.website_id );
end if;
end;

You need to change the delimiter from ; to something else, before defining any stored procedure/functions or triggers.
delimiter ||
create trigger queue after update on downloads
for each row
begin
if NEW.completed = 1 then
insert into s_queue ( website_id ) values ( NEW.website_id );
end if;
end||
delimiter;

Related

how to declare variable phpmyadmin mysql triggers properly?

I have a database where whenever residential address update in user table I want it to store in history table of user. For that I'm trying to write triggers but failing miserably in phpmyadmin. Also it's not giving me proper reason why so I can correct it. This is what I have done so far.
DROP TRIGGER IF EXISTS `record_history`;
CREATE TRIGGER `record_history` AFTER UPDATE ON `s_user`
FOR EACH ROW
BEGIN
DECLARE date_current datetime;
DECLARE residential_address varchar(1000);
SET #date_current = NOW();
SET #residential_address = NEW.residential_address;
IF (#residential_address <> OLD.residential_address AND #residential_address != "" AND #residential_address IS NOT NULL) THEN
INSERT INTO history_residential_address (`s_u_id`, `residential_address`, `status`, `date_added`, `date_updated`) VALUES
(OLD.s_u_id, #residential_address, 1, #date_current, #date_current);
END IF;
END;
delimiter ;
A cleaner version of your code
DROP TRIGGER IF EXISTS `record_history`;
delimiter $$
CREATE TRIGGER `record_history` AFTER UPDATE ON `s_user`
FOR EACH ROW
BEGIN
IF (new.residential_address <> OLD.residential_address AND new.residential_address <> "" AND new.residential_address IS NOT NULL) THEN
INSERT INTO history_residential_address (`s_u_id`, `residential_address`, `status`, `date_added`, `date_updated`) VALUES
(OLD.s_u_id, new.residential_address, 1, now(), now());
END IF;
END $$
delimiter ;
If you are still having problems please add sample data from s_user as text to the question.

undefined error with MySql insert trigger

I'm trying to write a MySQL insert trigger:
CREATE TRIGGER `trigger1` BEFORE INSERT ON `COMPANY`
FOR EACH ROW
IF NEW.DOKUMENTTYP LIKE 'test'
AND NEW.STATUS LIKE 'neu'
THEN
INSERT INTO my_tools.testimport( processname, version, step, insstring, initiator )
VALUES ( 'COMPANY_ER', 0, 1, CONCAT( 'DWDOCID=', NEW.DWDOCID ) , 'robot' );
END IF;
But there's an error in my SQL syntax. I cannot find the solution. Could anybody please help me?
Try this
IF (NEW.DOKUMENTTYP LIKE 'test' AND NEW.STATUS LIKE 'neu') ...
Triggers body contains more then one statement, it means that BEGIN..END keywords must be used. And add DELIMITERs, if needed:
DELIMITER $$
CREATE TRIGGER `trigger1`
BEFORE INSERT
ON `Company`
FOR EACH ROW
BEGIN
IF NEW.DOKUMENTTYP LIKE 'test' AND NEW.Status LIKE 'neu' THEN
INSERT INTO my_tools.testimport (processname, VERSION, step, insstring, initiator)
VALUES ('COMPANY_ER', 0, 1, CONCAT('DWDOCID=', NEW.DWDOCID), 'robot');
END IF;
END$$
DELIMITER ;

MySQL trigger, change value before update conditionally

I'm trying to change a value conditionally on my trigger, but I've got an error.
My trigger is:
CREATE TRIGGER `defineBase` BEFORE INSERT ON `perguntas`
FOR EACH ROW
BEGIN
IF NEW.per_base = 0 THEN
SET NEW.per_base = (SELECT per_id FROM perguntas ORDER BY per_id DESC LIMIT 1) + 1;
END IF;
END;
but doesn't work.
You need to change the delimiter to something else than ;. Otherwise the trigger definition stops at the first ;
delimiter |
CREATE TRIGGER `defineBase` BEFORE INSERT ON `perguntas`
FOR EACH ROW
BEGIN
IF NEW.per_base = 0 THEN
SET NEW.per_base = (SELECT per_id FROM perguntas ORDER BY per_id DESC LIMIT 1) + 1;
END IF;
END
|
delimiter ;
I also have the same problem. My SQL code before is just like this (without delimiter):
CREATE TRIGGER update_created_time BEFORE INSERT
ON contact FOR EACH ROW
BEGIN
SET NEW.created=NOW();
END
Then, after i add the following DELIMITER // and close it with the same //
DELIMITER //
CREATE TRIGGER update_created_time BEFORE INSERT
ON contact FOR EACH ROW
BEGIN
SET NEW.created=NOW();
END //
It works. I hope it can help someone in the future...

Generic logging with differences using triggers

I am trying to write a trigger that would simplify logging changes when a record is updated. I have a stored procedure that does an INSERT to a logging table called log_update and a trigger that does the job for 1 field:
DROP TRIGGER IF EXISTS `parents_update`;
DELIMITER $$
CREATE TRIGGER `parents_update` AFTER UPDATE ON `parents`
FOR EACH ROW
BEGIN
IF ( NEW.motherLastName != OLD.motherLastName ) THEN
CALL log_update( 'parent', NEW.id, 4, CONCAT( OLD.motherLastName, ' > ', NEW.motherLastName ) );
END IF;
END;
$$
DELIMITER ;
Is it a way to make this more generic like provide list of fields:
( 'motherFirstName', 'motherLastName', 'fatherFistName', .... )
and loop through this list with a parameter for the single IF statement?
Edit:
I come up with such loop:
DROP TRIGGER IF EXISTS `parents_update`;
DELIMITER $$
CREATE TRIGGER `parents_update` AFTER UPDATE ON `parents`
FOR EACH ROW
BEGIN
DECLARE _fieldName VARCHAR(25);
DECLARE done INT DEFAULT FALSE;
DECLARE fieldsCursor CURSOR FOR SELECT fieldName FROM log_fields WHERE tableName = 'parents';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN fieldsCursor;
the_loop : LOOP
FETCH fieldsCursor INTO _fieldName;
IF done THEN
LEAVE the_loop;
END IF;
-- _fieldName will contain values such 'motherLastName'
IF ( NEW ( _fieldName ) != OLD ( _fieldName ) ) THEN
CALL log_update( 'parent', NEW.id, 4, CONCAT( OLD ( _fieldName ), ' > ', NEW ( _fieldName ) ) );
END IF;
END LOOP the_loop;
CLOSE fieldCursor;
END;
$$
DELIMITER ;
where table log_fields will contain fields to check. Now I am facing the problem of how to access the NEW. or OLD. property if the field name is in a variable.
I would say you can use prepared statements to achieve it, but unfortunately it is not supported in triggers.
You can read more about it here: http://dev.mysql.com/doc/refman/5.1/en/stored-program-restrictions.html OR answer here: Alternative to using Prepared Statement in Trigger with MySQL
which mean you can't create dynamic SQL query in your trigger the same applied to NEW. or OLD. variables dinamic build, so the only way is create separate triggers for each table with listed all column names one by one

MySQL TRIGGER syntax, IF THEN INSERT

I want to modify a working trigger that looks like this:
CREATE TRIGGER j_update BEFORE UPDATE ON d
FOR EACH ROW
INSERT INTO j (
stampOld, stampNew
)
VALUES (
OLD.stamp, NEW.stamp
);
to execute only on the success of an IF statement. I can't get the right syntax for this modification.
I'm trying to achieve the following:
CREATE TRIGGER j_update BEFORE UPDATE ON d
FOR EACH ROW
IF (1=1) THEN
INSERT INTO d (
stampOld, stampNew
)
VALUES (
OLD.stamp, NEW.stamp
); /* <--- LINE 21 */
END IF;
Result: ERROR 1064: Check for right syntax near '' at line 21
Things I've tried:
wrapping the IF ... END IF; in BEGIN .. END;
switching delimiter to $$
I'm using MySQL 5.0.95 for RHEL.
Any help is greatly appreciated.
Try this
delimiter //
CREATE TRIGGER j_update BEFORE UPDATE ON d
FOR EACH ROW
BEGIN
IF 1=1 THEN
INSERT INTO d (
stampOld, stampNew
)
VALUES (
OLD.stamp, NEW.stamp
);
END IF;
END; //
delimiter ;
Here is one I just tried on my mysql but its for update almost same with your logic
mysql> delimiter //
mysql> create trigger tbl_two_ins
-> after insert on tabletwo
-> for each row
-> begin
-> if 1=1 then
-> update tableone t1 set t1.tb2_id = new.tab2_id where t1.tb1_id = new.tb1_id ;
-> end if ;
-> end ; //
Query OK, 0 rows affected (0.56 sec)
mysql> delimiter ;