mysql trigger syntax error - mysql

I need the right syntax for mysql trigger I'm using version 5.1
and the syntax error alwys apper when I wrote sql statment
DELIMITER $$
CREATE TRIGGER blood_year
AFTER INSERT ON donor
FOR EACH ROW
BEGIN
INSERT INTO blood_donation (donation_year)VALUES
(YEAR(NOW()));
END$$
any idea??

Try adding another "DELIMITER" statement at the end:
DELIMITER $$
CREATE TRIGGER blood_year
AFTER INSERT ON donor
FOR EACH ROW
BEGIN
INSERT INTO blood_donation (donation_year)VALUES (YEAR(NOW()));
END$$
DELIMITER ;

Related

Error in MySQL trigger after insert

I am creating trigger for after insert but I am getting error.
DELIMITER //
CREATE TRIGGER ParticleTableInsert
AFTER INSERT
ON Particle FOR EACH ROW
BEGIN
INSERT INTO Particles_Log(Message) VALUES ('Pace_Particles_Log');
END;
DELIMITER ;
I am new to mysql, sonot understanding what is wrong with my query. Please help.
DELIMITER //
CREATE TRIGGER ParticleTableInsert
AFTER INSERT
ON Particle FOR EACH ROW
BEGIN
INSERT INTO Particles_Log(Message) VALUES ('Pace_Particles_Log');
END//
DELIMITER;

Unable to execute Trigger in mysql

I have tables like CallLogs and Activities.Here,My requirement is for each insertion in CallLogs i need an entry in Activities.for that i am writing a trigger and procedure
I am calling procedure from my trigger
Procedure
DELIMITER $$
CREATE PROCEDURE `activity_insert` (IN text TEXT,IN type varchar(250),IN subtype varchar(250),IN date timestamp, IN url TEXT,IN target_id INT(16))
BEGIN
Insert into Activities(text,type,subtype,timestamp,url,Target_ID)values(in_text,in_type,in_subtype,in_date,in_url,in_target_id);
END $$
DELIMITER ;
It is executed successfully
Trigger
DROP TRIGGER IF EXISTS `call_logs_insert`
DELIMITER $$
CREATE TRIGGER `call_logs_insert` AFTER INSERT ON `CallLogs`
FOR EACH ROW
BEGIN
CALL activity_insert('message','Calls',NEW.'type',NEW.'date','null',NEW.'Target_ID');
END;
$$
for some of the columns i am giving static data. and for some of columns i am using new table data
When i am trying to execute this trigger i am getting a following 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 ''type',NEW.'date','null',NEW.'Target_ID'); END; $$' at line 4
Can any one plz tell me to execute this trigger what are the changes i need to do
Remove single quote from table columns
DROP TRIGGER IF EXISTS `call_logs_insert`
DELIMITER $$
CREATE TRIGGER `call_logs_insert` AFTER INSERT ON `CallLogs`
FOR EACH ROW
BEGIN
CALL activity_insert('message','Calls',NEW.type,NEW.date,'null',NEW.Target_ID);
END;
$$

MySQL trigger syntax error near CREATE TRIGGER

I'm trying to create trigger in Mysql, and get the error:
check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TRIGGER `foo`.`test` AFTER INSERT ON `foo`.`test` FOR EACH ROW BE' at line 2
in this trigger:
DELIMITER $$
SET #name2:='w';
CREATE TRIGGER `foo`.`test`
AFTER INSERT
ON `foo`.`test`
FOR EACH ROW BEGIN
SELECT name1 INTO name2;
END;
$$
DELIMITER ;
What is wrong with line 2?
try this:
DELIMITER $$
CREATE TRIGGER `foo`.`test` AFTER INSERT
ON `foo`.`test`
FOR EACH ROW BEGIN
SET #name2:='w';
SELECT name1 INTO #name2;
END$$
DELIMITER ;

MySQL Trigger Syntax Error #1064

I am trying to implement a trigger in MySQL that will automatically add/modify a column when another one is added/modified. Basically I want it to save a type as a slug in the type_sanitized column when a value is added/modified into type.
This is my initial try:
DROP TRIGGER IF EXISTS ArticleTypes.insert_sanitized_article_type;
CREATE TRIGGER 'insert_sanitized_article_type'
AFTER INSERT
ON 'ArticleTypes'
FOR EACH ROW
SET NEW.type_sanitized = LOWER(REPLACE(TRIM(NEW.type), ' ', '-'));
DROP TRIGGER IF EXISTS ArticleTypes.update_sanitized_article_type;
CREATE TRIGGER 'update_sanitized_article_type'
AFTER UPDATE
ON 'ArticleTypes'
FOR EACH ROW
SET NEW.type_sanitized = LOWER(REPLACE(TRIM(NEW.type), ' ', '-'));
After seeing suggestions online, I have tried adding delimiters like this:
DROP TRIGGER IF EXISTS ArticleTypes.insert_sanitized_article_type;
DELIMITER $$
CREATE TRIGGER 'insert_sanitized_article_type'
AFTER INSERT
ON 'ArticleTypes'
FOR EACH ROW
BEGIN
SET NEW.type_sanitized = LOWER(REPLACE(TRIM(NEW.type), ' ', '-'));
END$$
DELIMITER ;
DROP TRIGGER IF EXISTS ArticleTypes.update_sanitized_article_type;
DELIMITER $$
CREATE TRIGGER 'update_sanitized_article_type'
AFTER UPDATE
ON 'ArticleTypes'
FOR EACH ROW
BEGIN
SET NEW.type_sanitized = LOWER(REPLACE(TRIM(NEW.type), ' ', '-'));
END$$
DELIMITER ;
I get the following error for both of them:
#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 ''insert_sanitized_article_title'
AFTER INSERT
ON 'ArticleTypes'
FOR EACH ROW
BE' at line 1
This is my first time creating triggers in MySQL so it might be obvious but I haven't been able to figure it out in 2 hours of searching so any help would be greatly appreciated.
Remove the apostrophes.
DROP TRIGGER IF EXISTS ArticleTypes.insert_sanitized_article_type;
DELIMITER $$
CREATE TRIGGER insert_sanitized_article_type
AFTER INSERT
ON ArticleTypes
FOR EACH ROW
BEGIN
SET #type_sanitized = LOWER(REPLACE(TRIM(#type), ' ', '-'));
END$$
DELIMITER ;
DROP TRIGGER IF EXISTS ArticleTypes.update_sanitized_article_type;
DELIMITER $$
CREATE TRIGGER update_sanitized_article_type
AFTER UPDATE
ON ArticleTypes
FOR EACH ROW
BEGIN
SET #type_sanitized = LOWER(REPLACE(TRIM(#type), ' ', '-'));
END$$
DELIMITER ;

Perform insert or update on table using trigger

I am trying to fetch some values from php page and perform trigger in mysql database. Which is as below:
DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `test`.`MysqlTrigger`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `MysqlTrigger` AFTER INSERT OR UPDATE ON `table2`
FOR EACH ROW BEGIN
IF(NEW .flag=0) THEN
INSERT INTO temp(sent,pcount,ncount) VALUES (NEW.sent,NEW.pcount,NEW.ncount);
ELSE
UPDATE temp SET pcount=NEW.pcount AND ncount=NEW.ncount WHERE id = NEW.id;
END IF;
END;
$$
DELIMITER ;
Which gives 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 'or update ON `table2`
FOR EACH ROW BEGIN
if(NEW .flag=0) then
INSERT INTO' at line 3
I tried to change = with =: but error persist. Any idea whaat the prob?
You can't define a trigger for update and insert at the same time. You have to define 2 triggers:
CREATE TRIGGER `Mysql_insert_Trigger` AFTER INSERT ON `table2` ...
CREATE TRIGGER `Mysql_update_Trigger` AFTER UPDATE ON `table2` ...
I count do it. There was problem in syntax. Here is the correct which perform update insert at same moment in different table and update-inset on same table but with if else:
DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `test`.`MysqlTrigger2`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `MysqlTrigger2` AFTER INSERT ON `table2`
FOR EACH ROW BEGIN
IF 'flag'=1 THEN
UPDATE record SET sent=NEW.sent WHERE id=NEW.id;
ELSE
INSERT INTO record VALUES (id,NEW.sent);
INSERT INTO temp VALUES ()
END IF;
END;
$$
DELIMITER ;