I am trying to write a trigger where if an incoming value is empty (in other words ''), then insert NULL in the table. I have :
DELIMITER //
CREATE TRIGGER avoid_empty
BEFORE INSERT ON EVALUATION
FOR EACH ROW
BEGIN
IF mark = '' THEN SET NEW.mark = NULL;
END IF;
END;
//
DELIMITER ;
Which executes without errors, but it doesn't do what I need.
Try:
DELIMITER //
CREATE TRIGGER `avoid_empty` BEFORE INSERT ON `EVALUATION`
FOR EACH ROW
BEGIN
IF NEW.`mark` = '' THEN
SET NEW.`mark` := NULL;
END IF;
END//
DELIMITER ;
See db-fiddle.
DELIMITER $$
DROP TRIGGER IF EXISTS TRIGGER_BEFORE_USERS_MAUALEXPIRY_USEREXPIRAY_UPDATE $$
CREATE TRIGGER TRIGGER_BEFORE_USERS_MAUALEXPIRY_USEREXPIRAY_UPDATE
BEFORE UPDATE ON USERS
FOR EACH ROW
BEGIN
IF #DISABLE_TRIGGERS IS NULL THEN
IF NOT (NEW.MANUALEXPIRATIONDATE <=> OLD.MANUALEXPIRATIONDATE) THEN
SET NEW.CUSTOMFIELD2 = NEW.MANUALEXPIRATIONDATE;
IF NOT(NEW.USEREXPIRYDATE <=> OLD.USEREXPIRYDATE) THEN
SET NEW.CUSTOMFIELD3 = NEW.USEREXPIRYDATE;
END IF;
END IF;
END$$
DELIMITER ;
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 '' at line 17
CREATE TRIGGER TRIGGER_BEFORE_USERS_MAUALEXPIRY_USEREXPIRAY_UPDATE
BEFORE UPDATE ON USERS
FOR EACH ROW
BEGIN
IF #DISABLE_TRIGGERS IS NULL THEN
IF NOT (NEW.MANUALEXPIRATIONDATE <=> OLD.MANUALEXPIRATIONDATE) THEN
SET NEW.CUSTOMFIELD2 = NEW.MANUALEXPIRATIONDATE;
END IF;
IF NOT(NEW.USEREXPIRYDATE <=> OLD.USEREXPIRYDATE) THEN
SET NEW.CUSTOMFIELD3 = NEW.USEREXPIRYDATE;
END IF;
END IF;
END
$$
I have created the following trigger in mysql:
DELIMITER $$
CREATE TRIGGER `some_trigger` AFTER INSERT ON db1.table
FOR EACH ROW
BEGIN
IF NEW.userid = 'certain_id' THEN
INSERT INTO db2.table
SET
value1 = NEW.value,
value2 = NEW.value;
END IF;
END $$
DELIMITER ;
The above statement works if I remove the if statement. Additionally no syntax errors are encountered when this trigger is added to the db. Any idea what is wrong with the if statement that is not allowing it to insert entries with value 'certain_id' in column userid when added to db1.table??
Try this it should work..
DELIMITER $$
CREATE TRIGGER `some_trigger` AFTER INSERT ON `db1`.`table`
FOR EACH ROW
BEGIN
IF (NEW.userid = 'certain_id') THEN
INSERT INTO `db2`.`table` (value1, value2)
VALUES (NEW.value1,NEW.value2);
END IF;
END $$
DELIMITER ;
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'));
#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;