Syntax error if statement mySQL - mysql

create trigger tr_af after update on relatorio_notas
for each row
if (new.Nota < 7) then
insert into aluno_af (nome, matricula) values (new.Nota, new.Matricula_estudante)
end if;
I got syntax error and I don't know why

You need a BEGIN and END block in your syntax. See the 13.1.11 CREATE TRIGGER Syntax:
create trigger tr_af after update on relatorio_notas
for each row
begin # <-------------------
if (new.Nota < 7) then
insert into aluno_af (nome, matricula) values (new.Nota, new.Matricula_estudante);
end if;
end # <-------------------
Note you may need to set the delimiter to something different than ;.
See more info in MySQL syntax check or a sample in Trigger syntax and IF ELSE THEN.

Related

sql syntax error creating trigger

I want to create trigger and I have written this query but this does not execute. Please check my query
CREATE
TRIGGER 'blog_after_insert' AFTER INSERT
ON 'blog'
FOR EACH ROW BEGIN
IF NEW.deleted THEN
SET #changetype = 'DELETE';
ELSE
SET #changetype = 'NEW';
END IF;
INSERT INTO audit (blog_id, changetype) VALUES (NEW.id, #changetype);
I am getting this 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 ''blog_after_insert' AFTER INSERT
ON 'blog'
FOR EACH ROW BEGIN
IF NEW.del' at line 2
Please run this query
DELIMITER $$
CREATE
TRIGGER blog_after_insert AFTER INSERT
ON blog
FOR EACH ROW BEGIN
IF NEW.deleted THEN
SET #changetype = "DELETE";
ELSE
SET #changetype = "NEW";
END IF;
INSERT INTO audit (blog_id, changetype) VALUES (NEW.id, #changetype);
END$$
DELIMITER ;
Single quotes (') denote string literals - object names, like triggers and tables should use forward quotes, or no quotes at all:
CREATE
TRIGGER blog_after_insert AFTER INSERT -- here
ON blog -- and here
FOR EACH ROW BEGIN
-- rest of code...

Error SQL syntax in a trigger, MySQL

I receive this 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 6" but can not figure out what is wrong.
(position and points are MEDIUMINT, they are not primary key neither unique)
Anyone?
CREATE TRIGGER pointsAssigns
before INSERT ON MyTable
FOR EACH ROW
BEGIN
IF NEW.position>6 THEN
set NEW.points=5;
END IF;
END;
As #Mihai mentioned either add closing END and change the DELIMITER
DELIMITER //
CREATE TRIGGER pointsAssigns
BEFORE INSERT ON MyTable
FOR EACH ROW
BEGIN
IF NEW.position > 6 THEN
SET NEW.points = 5;
END IF;
END //
DELIMITER ;
Here is a SQLFiddle demo
or make it one-line trigger and then you don't need neither BEGIN...END block nor changing the DELIMITER
CREATE TRIGGER pointsAssigns
BEFORE INSERT ON MyTable
FOR EACH ROW
SET NEW.points = IF(NEW.position > 6, 5, NEW.points);
Here is a SQLFiddle demo

SQL syntax error near '' at line 14

I have created this trigger in HeidiSQL IDE for mysql trough IDE´s helper and it worked pretty good. If I copy the create code generated by the IDE and try to run it on phpmyadmin I get a SQL syntax error that I just can´t figure how to fix it. Can anyone help me?
CREATE TRIGGER `teste` AFTER UPDATE ON `ilmug_virtuemart_products` FOR EACH ROW BEGIN
IF (NEW.origem_sync <> 0) THEN
INSERT INTO sincronizar (dataHora,
tipoMovimento,
entidade,
id,
version,
STATUS)
VALUES (CURRENT_TIMESTAMP(),
'update',
'virtuemart_products',
NEW.virtuemart_product_id,
NEW.version,
'pendente');
END IF;
END;
You have a BEGIN statement...but the END is missing
Since you have edited your question, I tested it: http://sqlfiddle.com/#!2/0ebbf
According to sqlfiddle, there is no syntax error...
Use DELIMITER.
DELIMITER $$
CREATE TRIGGER `teste` AFTER UPDATE ON `ilmug_virtuemart_products` FOR EACH ROW
BEGIN
IF (NEW.origem_sync <> 0) THEN
INSERT INTO sincronizar (dataHora,
tipoMovimento,
entidade,
id,
version,
STATUS)
VALUES (CURRENT_TIMESTAMP(),
'update',
'virtuemart_products',
NEW.virtuemart_product_id,
NEW.version,
'pendente');
END IF;
END$$
DELIMITER ;

syntax error in mysql 'trigger'

I am just trying to use trigger instead of check constraint and code one but it gives me an error.
CREATE TRIGGER conflict
ON roozane
FOR EACH ROW
BEGIN
if rDate = NEW.rDate then
if NEW.rStartTime < rStartTime AND NEW.rEndTime < rEndTime then
INSERT INTO roozane (rID,rDate,rStartTime,rEndTime,rPlace,rComment,rType) values (NEW.rID,NEW.rDate,NEW.rStartTime,NEW.rEndTime,NEW.rPlace,NEW.rComment,NEW.rType);
end if
end if
END;$$
And 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 'ON roozane FOR EACH ROW BEGIN if ( rDate=NEW.rDate ) then if ( NEW.rStart' at line 2
EDIT
CREATE TRIGGER conflict BEFORE INSERT
ON roozane
FOR EACH ROW
BEGIN
if rDate = NEW.rDate then
if NEW.rStartTime < rStartTime AND NEW.rEndTime < rEndTime then
INSERT INTO roozane (rID,rDate,rStartTime,rEndTime,rPlace,rComment,rType) values (NEW.rID,NEW.rDate,NEW.rStartTime,NEW.rEndTime,NEW.rPlace,NEW.rComment,NEW.rType);
end if
end if
END;$$
and the 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 7
tnx for help
You need a *trigger_time* and *trigger_event*. For example:
CREATE TRIGGER conflict AFTER INSERT
You need a semicolon after each end if to terminate those compound statements.
You don't need a semicolon after the last END, because presumably you have used DELIMITER $$ to change the statement terminator in the mysql client.
I tested the following. It did not get a syntax error, but of course I have no table called roozane so I got a different error. :-)
CREATE TRIGGER conflict BEFORE INSERT
ON roozane
FOR EACH ROW
BEGIN
if rDate = NEW.rDate then
if NEW.rStartTime < rStartTime AND NEW.rEndTime < rEndTime then
INSERT INTO roozane (rID,rDate,rStartTime,rEndTime,rPlace,rComment,rType) values (NEW.rID,NEW.rDate,NEW.rStartTime,NEW.rEndTime,NEW.rPlace,NEW.rComment,NEW.rType);
end if;
end if;
END$$
You have several problems with your trigger.
As far as syntactical and logic errors go
Looking at the error message apparently you didn't use DELIMITER $$ at the beggining of your script.
You have three undeclared variables in your trigger rDate, rStartTime, rEndTime. If you use stored procedure level variables you need to declare them first and eventually assign values to them.
As #BillKarwin mentioned in his answer you have to have semicolons at the end of each IF ... END IF; statement and you don't need semicolon after closing END of a BEGIN...END block of your trigger since you should've changed DELIMITER earlier to $$.
That being said syntactically correct version of your trigger might be following
DELIMITER $$
CREATE TRIGGER conflict
BEFORE INSERT ON roozane
FOR EACH ROW
BEGIN
DECLARE rDate DATE;
DECLARE rStartTime TIME;
DECLARE rEndTime TIME;
IF rDate = NEW.rDate THEN
IF NEW.rStartTime < rStartTime AND NEW.rEndTime < rEndTime THEN
INSERT INTO roozane (rID,rDate,rStartTime,rEndTime,rPlace,rComment,rType)
VALUES(NEW.rID,NEW.rDate,NEW.rStartTime,NEW.rEndTime,NEW.rPlace,NEW.rComment,NEW.rType);
END IF;
END IF;
END$$
Here is SQLFiddle demo that shows that now your trigger is being successfully created but does nothing since declared variables by default have values of NULL and other values have not been assigned to them.
Here goes the most important part: event if the problem with the variables will be fixed unfortunately your trigger won't work anyway because MySql with its rather limited support for triggers doesn't allow data manipulation statements (INSERT in your case) on the same table (roozane in your case) you are attaching your trigger to.
Now, to help you to fix your trigger you need to explain what you want your trigger to check for.

move trigger from one table to another trouble with syntax

I had to restore a table from a backed-up version of that table. Then I went to recreate the triggers. I can't get one re-created. I've looked at some examples of how this sort of trigger should look nothing here glares at me. I copied it from the code showed in phpmyadmin when i clicked "change" on the old trigger to save it. I thought that would be foolproof!
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
CREATE TRIGGER `trig_parts_price_AfterUpdate` AFTER UPDATE ON `parts`
FOR EACH ROW BEGIN
IF (NEW.cost <> OLD.cost || NEW.price <> OLD.price || NEW.QtyInStock <> OLD.QtyInStock) THEN
INSERT INTO `inv_hist_simple` (sku, cost, price, QtyInStock, change_date)
VALUES(OLD.sku_m, NEW.cost, NEW.price, NEW.QtyInStock, NOW());
END IF;
END;
Put this line above your code:
DELIMITER $$
and change the last line of your code to
END $$
Then add this line at the end:
DELIMITER ;
Otherwise the ; tells the server, that the code for the trigger finishes after your insert statement, which is causing the syntax error.
So it basically should look like this:
DELIMITER $$
CREATE TRIGGER `trig_parts_price_AfterUpdate` AFTER UPDATE ON `parts`
FOR EACH ROW BEGIN
IF (NEW.cost <> OLD.cost || NEW.price <> OLD.price || NEW.QtyInStock <> OLD.QtyInStock) THEN
INSERT INTO `inv_hist_simple` (sku, cost, price, QtyInStock, change_date)
VALUES(OLD.sku_m, NEW.cost, NEW.price, NEW.QtyInStock, NOW());
END IF;
END $$
DELIMITER ;