MySQL Trigger Syntax Error - mysql

Running on MySQL 5.5.9 with InnoDB.
I created the following trigger:
CREATE TRIGGER TRIGGER_Products_Insert
AFTER INSERT ON Products
FOR EACH ROW
BEGIN
UPDATE Products
SET current = 0
WHERE id = new.id
AND current = 1
AND autonumber <> new.autonumber
END;
MySQLWorkbench shows a syntax error on the last line, and executing this throws 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 'END' at line 11
Where is my error?

http://dev.mysql.com/doc/refman/5.5/en/create-trigger.html
DELIMITER |
CREATE TRIGGER TRIGGER_Products_Insert AFTER INSERT ON Products
FOR EACH ROW BEGIN
UPDATE Products
SET current = 0
WHERE id = new.id
AND current = 1
AND autonumber <> new.autonumber;
END;
|
DELIMITER ;

You are missing a ; before your END keyword:
CREATE TRIGGER TRIGGER_Products_Insert
AFTER INSERT ON Products
FOR EACH ROW
BEGIN
UPDATE Products
SET current = 0
WHERE id = new.id
AND current = 1
AND autonumber <> new.autonumber;
END;

Related

Can I update just added row in MySQL with trigger based on condition?

I need to update the salary of a newly added employee in the same table if condition is satisfied. SQL keeps throwing errors about syntax, if I use delimiter change it looks like this:
delimiter //
CREATE TRIGGER fixSalary
AFTER INSERT ON emp
FOR EACH ROW
BEGIN
IF NEW.salary < 50000 THEN
UPDATE emp
SET salary = 5000
WHERE eno = NEW.eno;
END IF;
END;//
delimiter ;
But it throws an error:
"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
'delimiter //
create trigger fixSalary
after insert on emp
for each row
begin
if ' at line 1"
I tried removing the delimiter:
CREATE TRIGGER fixSalary
AFTER INSERT ON emp
FOR EACH ROW
BEGIN
IF NEW.salary < 50000 THEN
UPDATE emp
SET salary = 5000
WHERE eno = NEW.eno;
END IF;
END;
which resulted in another error:
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 8
I've tried all kinds of semicolons combinations with no luck. Can anyone help me to understand what's going on?

mysql:ERROR 1064 (42000) in trigger

This is a trigger i write.
DELIMITER //
create trigger after_insert_bid
after insert on Bid
for each row
when exists(
select *
from Item
where ItemID = new.ItemID)
begin
update Item set Item.currently = NEW.amount where Item.itemID = NEW.itemID;
end;//
DELIMITER ;
It has an error:
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 'when exists(
SELECT *
FROM Item
WHERE ItemID = new.ItemID
)
begin
UPDATE Item SE' at line 4
mysql> end;
I am new to mysql, and i know it's an simple question, but i really don't know how to find the error, thank you!
mysql version is 5.1.73
You don't need to check whether the itemid exists in item table just update the item table directly. The table will not be updated if ItemID do not exists in Item table.
for each row
begin
update Item set Item.currently = NEW.amount where Item.itemID = NEW.itemID;
end

MySQL AFTER UPDATE TRIGGER to increase count

Any ideas what's wrong with this query? I searched around stackoverflow and it seems everyone is using this and it works.
mysql> CREATE TRIGGER upd_entry
-> AFTER UPDATE ON entry FOR EACH ROW
-> BEGIN
-> UPDATE entry
-> SET count = count +1
-> WHERE id = NEW.id
-> END;
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 'END' at line 7
Run this first to change the delimiter
DELIMITER //
create the trigger
CREATE TRIGGER upd_entry
AFTER UPDATE ON entry FOR EACH ROW
BEGIN
UPDATE entry
SET count = count +1
WHERE id = NEW.id;
END//
After change it back
DELIMITER ;
forget ; at end of line
CREATE TRIGGER upd_entry
AFTER UPDATE ON entry FOR EACH ROW
BEGIN
UPDATE entry SET count = count +1 WHERE id = NEW.id;//forget to close line ;
END;//
delimiter;
Mysql Trigger

MySQL trigger insert

I am attempting to increment count in my grade_distribution table whenever a new row is inserted into my Grades table. When the grade inserted into Grades is the same as the grade in grade_distribution, the count should be incremented.
But i am getting an error:
ERROR 1064 (42000) at line 1: 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 6
create trigger tr_grades_insert after insert on Grades
for each row
begin
update grade_distribution
set grade_distribution.count = grade_distribution.count+1
where Grades.Grade = grade_distribution.grade;
end
I needed a delimiter to end the trigger. Also to use update students.grade_distribution
DELIMITER //
create trigger tr_grades_insert after insert on Grades
for each row
begin
update students.grade_distribution
set count=count+1
where grade = NEW.Grade;
end//
DELIMITER ;

mysql error with a trigger

I want to create a trigger which will be executed when a new row is about to be inserted if my condition is satisfied ( latest version = 1 ) all previous row will be updated with latest version = 0
CREATE TRIGGER remiseazero
BEFORE INSERT
ON wp_stattype2_3_activite
FOR EACH ROW
BEGIN
IF NEW.latestversion = 1
THEN
update wp_stattype2_3_activite
set latestversion = 0 where typecas = new.typecas;
END IF;
END;$$
Error:
#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
Put delimiter. And remove ; after END.
DELIMITER $$
CREATE TRIGGER remiseazero
BEFORE INSERT
ON wp_stattype2_3_activite
FOR EACH ROW
BEGIN
IF (NEW.latestversion = 1) THEN
update wp_stattype2_3_activite set latestversion = 0 where typecas = NEW.typecas;
END IF;
END$$
DELIMITER ;