MYSQL Error when creating a trigger - mysql

I have the following MySQL trigger query :
CREATE
TRIGGER `after_insert_stock` AFTER INSERT
ON `stock`
FOR EACH ROW BEGIN
IF NEW.deleted THEN
SET #changetype = 'DELETE';
ELSE
SET #changetype = 'NEW';
END IF;
INSERT INTO stock_audit (stock_id, commodity_name,commodity_id,delivery_no,supplier_name,batch_no,expiry_date,units_per_pack,no_of_packs,total_quantity,buying_price,selling_price,remarks,available_quantity,user_id,changetype) VALUES (NEW.stock_id,NEW.commodity_name,NEW.commodity_id,NEW.delivery_no,NEW.supplier_name,NEW.batch_no,NEW.expiry_date,NEW.units_per_pack,NEW.total_quantity,NEW.buying_price,NEW.selling_price,NEW.remarks,NEW.available_quantity,NEW.user_id, #changetype);
END$$
When I run the Query I get the following MySQL error : Error
SQL query:
CREATE TRIGGER `after_insert_stock` AFTER INSERT ON `stock_audit`
FOR EACH
ROW BEGIN
IF NEW.deleted
THEN
SET #changetype = 'DELETE';
MySQL said: Documentation
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
Please can some one help solving the problem?

You've missed DELIMITER declaration:
DELIMITER $$
CREATE
TRIGGER `after_insert_stock` AFTER INSERT
ON `stock`
FOR EACH ROW BEGIN
IF NEW.deleted THEN
SET #changetype = 'DELETE';
ELSE
SET #changetype = 'NEW';
END IF;
INSERT INTO stock_audit (stock_id, commodity_name,commodity_id,delivery_no,supplier_name,batch_no,expiry_date,units_per_pack,no_of_packs,total_quantity,buying_price,selling_price,remarks,available_quantity,user_id,changetype) VALUES (NEW.stock_id,NEW.commodity_name,NEW.commodity_id,NEW.delivery_no,NEW.supplier_name,NEW.batch_no,NEW.expiry_date,NEW.units_per_pack,NEW.total_quantity,NEW.buying_price,NEW.selling_price,NEW.remarks,NEW.available_quantity,NEW.user_id, #changetype);
END$$

Try this:
DELIMITER $$
CREATE TRIGGER after_insert_stock AFTER INSERT ON stock FOR EACH ROW BEGIN
DECLARE changetype varchar;
IF NEW.deleted THEN
SET changetype := 'DELETE';
ELSE
SET changetype := 'NEW';
END IF;
INSERT INTO stock_audit (stock_id, commodity_name,commodity_id,delivery_no,supplier_name,batch_no,expiry_date,units_per_pack,no_of_packs,total_quantity,buying_price,selling_price,remarks,available_quantity,user_id,changetype) VALUES (NEW.stock_id,NEW.commodity_name,NEW.commodity_id,NEW.delivery_no,NEW.supplier_name,NEW.batch_no,NEW.expiry_date,NEW.units_per_pack,NEW.total_quantity,NEW.buying_price,NEW.selling_price,NEW.remarks,NEW.available_quantity,NEW.user_id, :changetype);
END$$

Try this simplified query -
CREATE TRIGGER `after_insert_stock`
AFTER INSERT
ON `stock`
FOR EACH ROW
INSERT INTO
stock_audit (stock_id, commodity_name,commodity_id,delivery_no,supplier_name,batch_no,expiry_date,units_per_pack,no_of_packs,total_quantity,buying_price,selling_price,remarks,available_quantity,user_id,changetype)
VALUES
(NEW.stock_id,NEW.commodity_name,NEW.commodity_id,NEW.delivery_no,NEW.supplier_name,NEW.batch_no,NEW.expiry_date,NEW.units_per_pack,NEW.total_quantity,NEW.buying_price,NEW.selling_price,NEW.remarks,NEW.available_quantity,NEW.user_id, IF(NEW.deleted, 'DELETE', 'NEW'));

Related

How to create a BEFORE INSERT Trigger

I'm trying to create a BEFORE INSERT trigger and getting errors that I don't understand.
The MySQL to create the trigger looks like this:
CREATE TRIGGER `GetPolyLinkID` BEFORE INSERT ON `TableA`
FOR EACH ROW
BEGIN
INSERT INTO TableB () VALUES ();
NEW.PolyLinkID = last_insert_id();
END
The error I'm getting is
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 1
You forgot SET ... begore NEW.PolyLinkID = last_insert_id();
And
For creating complex trigger , you must use the delimiter keyword .
This is a example from rom https://dev.mysql.com/doc/refman/5.6/en/trigger-syntax.html .
delimiter //
CREATE TRIGGER upd_check BEFORE UPDATE ON account
FOR EACH ROW
BEGIN
IF NEW.amount < 0 THEN
SET NEW.amount = 0;
ELSEIF NEW.amount > 100 THEN
SET NEW.amount = 100;
END IF;
END;
//
delimiter ;
Screen shot with sequel pro

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...

Can't Create MySQL TRIGGER when i use IS NOT NULL

CREATE TRIGGER b_I_O AFTER UPDATE ON book
FOR EACH ROW
BEGIN
IF (OLD.status IS NOT NULL AND NEW.status IS NOT NULL AND NEW.status != OLD.status ) THEN
INSERT INTO book_in_out (astatus, bid, time) VALUES(NEW.status, OLD.id, NOW());
END IF;
END;
First post here after hours of searching.
I'm trying to insert into book_in_out using trigger after updating booking table, I was able to do this but if I update bookName the trigger will try to run.
I only want the trigger to run only when status column changes. default value for status column is NULL
Below is the error i keep getting:
#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 may try this
delimiter //
CREATE TRIGGER b_I_O AFTER UPDATE ON book
FOR EACH ROW
BEGIN
IF (OLD.status IS NOT NULL AND NEW.status IS NOT NULL AND NEW.status != OLD.status ) THEN
INSERT INTO book_in_out (`astatus`, `bid`, `time`) VALUES(NEW.status, OLD.id, NOW());
END IF;
END;//
delimiter ;
Try to manually specify delimeter for your trigger so it won't be confused with semicolons used within BEGIN/END, i.e.
DELIMITER //
CREATE TRIGGER b_I_O AFTER UPDATE ON book
FOR EACH ROW
BEGIN
IF (OLD.status IS NOT NULL AND NEW.status IS NOT NULL AND NEW.status != OLD.status ) THEN
INSERT INTO book_in_out (astatus, bid, time) VALUES(NEW.status, OLD.id, NOW());
END IF;
END;//

Else-If not working in trigger definition

#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 12
delimiter |
CREATE TRIGGER `pointhistorytrigger` AFTER UPDATE ON `points`
FOR EACH ROW BEGIN
IF NEW.is_open='4'
THEN
INSERT into point_history (`idpoints`,`idmembers`,`action_time`,`action_type`)
VALUES (OLD.idpoints,NEW.idmembers,NOW(),'3');
ELSE IF NEW.is_open='3'
THEN
INSERT into point_history (`idpoints`,`idmembers`,`action_time`,`action_type`)
VALUES (OLD.idpoints,NEW.idmembers,NOW(),'2');
END IF;
END;
| delimiter ;
delimiter |
CREATE TRIGGER `pointhistorytrigger`
AFTER UPDATE ON `points`
FOR EACH ROW
BEGIN
IF NEW.is_open='4' THEN
INSERT into point_history (`idpoints`,`idmembers`,`action_time`,`action_type`)
VALUES (OLD.idpoints,NEW.idmembers,NOW(),'3');
ELSEIF NEW.is_open='3' THEN
INSERT into point_history (`idpoints`,`idmembers`,`action_time`,`action_type`)
VALUES (OLD.idpoints,NEW.idmembers,NOW(),'2');
END IF;
END | -- <<== remove the semi colon here
delimiter ;
MySQL IF Syntax
If you are using an else-if in MySQL you have to use ELSEIF instead of ELSE IF. Everything else is fine in your statement.
You need 2 end if , one for if and another for else if...So your final trigger will be like this..
DELIMITER |
CREATE TRIGGER `pointhistorytrigger` AFTER UPDATE ON `points` FOR EACH ROW
BEGIN
IF NEW.is_open='4' THEN
INSERT into point_history (`idpoints`,`idmembers`,`action_time`,`action_type`) VALUES (OLD.idpoints,NEW.idmembers,NOW(),'3');
ELSE IF NEW.is_open='3' THEN
INSERT into point_history (`idpoints`,`idmembers`,`action_time`,`action_type`) VALUES (OLD.idpoints,NEW.idmembers,NOW(),'2');
END IF;
END IF;
END;

Syntax error when creating trigger

I'm trying to create a new trigger in MySQL but no matter what I try I'm getting syntax error. I want to insert a value into the html_id column which would be a concatenation of letter f and column id:
CREATE TRIGGER htmlid
BEFORE INSERT ON makelist_food
FOR EACH ROW
BEGIN
IF (NEW.html_id IS NULL) THEN
NEW.html_id = CONCAT('f', NEW.id);
END IF;
END
I also tried this:
CREATE TRIGGER htmlid
BEFORE INSERT ON makelist_food
FOR EACH ROW
BEGIN
IF (NEW.html_id IS NULL) THEN
INSERT INTO makelist_food SET html_id = CONCAT('f', NEW.id);
END IF;
END
And this (changing delimiter):
DELIMITER $$
CREATE TRIGGER htmlid
BEFORE INSERT ON makelist_food
FOR EACH ROW
BEGIN
IF (NEW.html_id IS NULL) THEN
NEW.html_id = CONCAT('f', NEW.id);
END IF;
END$$
DELIMITER ;
The error I'm getting: #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 '.html_id = CONCAT('f', NEW.id)' at line 6
I'm running MySQL 5.5.22.
Try with this query:
delimiter |
CREATE TRIGGER htmlid
BEFORE INSERT ON makelist_food
FOR EACH ROW
BEGIN
IF (NEW.html_id IS NULL) THEN
INSERT INTO makelist_food SET html_id = CONCAT('f', NEW.id);
END IF;
END
|
delimiter ;
I think the difference is the SET keyword. try this
DELIMITER $$
CREATE TRIGGER htmlid
BEFORE INSERT ON makelist_food
FOR EACH ROW
BEGIN
IF (NEW.html_id IS NULL) THEN
SET NEW.html_id = CONCAT('f', NEW.id);
END IF;
END$$
DELIMITER ;