I have two table stock detail and purchase.
When I'm trying to create a trigger on the table purchase it will seems an error that
"#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 "
Hear is my code
create TRIGGER afterinsert after insert on purchase for each row
BEGIN
set #qnty=(select quantity from stock_detail where brand_name = new.brand);
if(#qnty>=1) then
update stock_detail set quantity=#qnty+new.quanty;
end if;
END;
Try this:
delimiter //
create TRIGGER afterinsert after insert on purchase for each row
BEGIN
set #qnty=(select quantity from stock_detail where brand_name = new.brand);
if(#qnty>=1) then
update stock_detail set quantity=#qnty+new.quanty;
end if;
END;//
delimiter ;
For more info refer: http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html
Related
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?
Here is my trigger, I am getting the MySQL syntax error. I wanted to reduce the balance from sms_index table sms_count column value.
CREATE TRIGGER sms_log_update AFTER UPDATE ON sms_index
FOR EACH ROW
BEGIN
IF NEW.approved_status = '1' THEN
UPDATE sms_package SET balance = (balance - OLD.sms_count) WHERE group_id = OLD.ins_group_id;
END IF;
END;
Error Message:
#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 5
Your code looks correct, except for the possible problem of the delimiter. Try the following:
DELIMITER //
CREATE TRIGGER sms_log_update AFTER UPDATE ON sms_index
FOR EACH ROW
BEGIN
IF NEW.approved_status = '1' THEN
UPDATE sms_package SET balance = (balance - OLD.sms_count)
WHERE group_id = OLD.ins_group_id;
END IF;
END;//
DELIMITER ;
From the documentation:
However, just as for stored routines, if you use the mysql program to define a trigger that executes multiple statements, it is necessary to redefine the mysql statement delimiter so that you can use the ; statement delimiter within the trigger definition.
I'm writing a trigger in MySQL to take log of table updates. The log table is called individuo_storico and the target table is called individuo. When individuo is updated, I want to check if IDQualifica and IDLivello are changed, if yes a record in individuo_storico is inserted.
I write down this code but I get a #1064 error, where's the syntax error?
use ore;
create trigger individuo_update after update on individuo
for each row
begin
if ( NEW.IDLivello <> OLD.IDLivello or NEW.IDQualifica <> OLD.IDQualifica) then
insert into individuo_storico(IDIndividuo, IDQualifica, IDLivello)
values (NEW.IDIndividuo, NEW.IDQualifica, NEW.IDLivello);
end if;
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 6
Please wrap your trigger creation with the monikers necessary so the db engine does not choke on it. So three areas:
1) line 1
2) right after the end;
3) and then a reset to a default delimiter.
Same concept is typical with stored proc creations.
DELIMITER $$
create trigger individuo_update after update on individuo
for each row
begin
if ( NEW.IDLivello <> OLD.IDLivello or NEW.IDQualifica <> OLD.IDQualifica) then
insert into individuo_storico(IDIndividuo, IDQualifica, IDLivello)
values (NEW.IDIndividuo, NEW.IDQualifica, NEW.IDLivello);
end if;
end;$$
DELIMITER ;
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
Hi i'm trying to create below trigger
CREATE TRIGGER TRIGBEFORE INSERT ON employee
FOR EACH
ROW
BEGIN
UPDATE employee SET userId = userId +1 WHERE userId >1;
END
it is giving me below mysql error, please suggest what is wrong in it.
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 5
you forgot to set the delimiter and misspelled a word:
delimiter |
CREATE TRIGGER TRIG BEFORE INSERT ON employee
FOR EACH ROW BEGIN
UPDATE employee SET userId = userId +1 WHERE userId >1;
END;
|
delimiter ;
If you don't set another delimiter than ; the statement will end at the first ; and your trigger definition will be incomplete. You need to tell MySQL that the stamentment should end at the delimiter you defined. After that you can set the delimiter back with delimiter ;
I think you have a typo mistake TRIGBEFORE: manual here
CREATE TRIGGER trigger_name BEFORE INSERT ON ...