enter code here
DELIMITER $$
DROP TRIGGER IF EXISTS `Update_Status`$$
CREATE TRIGGER `Update_Status` AFTER INSERT ON `occurance_time`
FOR EACH ROW BEGIN
IF NOT EXISTS (SELECT `F_Seen` FROM `Total_Hours` WHERE (`SSN`=new.`SSN` and `Day_Date`=new.`Day_Date`))
THEN
INSERT INTO `total_time` (`SSN`,`Name`,`Day_Date`,`F_Seen`) VALUES(new.`SSN`,new.`Name`,new.`Day_Date`,new.`Cap_time`);
ELSE
UPDATE `total_time` SET(`L_Seen`=new.`Cap_time`) WHERE (`SSN`=new.`SSN` and `Day_Date`=new.`Day_Date`);
END$$
I have Created this After insert trigger On Occurrence _time I want to store first time of occurrence of a day and last time of occurrence of Day in Total_time table but getting this Error
"1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(L_Seen=new.Cap_time) WHERE (SSN=new.SSN and Day_Date=new.`Day_Date...' at line 7" ***
Instead of SET(L_Seen=new.Cap_time) Please use SET L_Seen=new.Cap_time. You missed end if also. Below code is working.
CREATE TRIGGER `Update_Status` AFTER INSERT ON `occurance_time`
FOR EACH ROW
BEGIN
IF NOT EXISTS (SELECT `F_Seen` FROM `Total_Hours` WHERE `SSN`=new.`SSN` and `Day_Date`=new.`Day_Date`)
THEN
INSERT INTO `total_time` (`SSN`,`Name`,`Day_Date`,`F_Seen`) VALUES(new.`SSN`,new.`Name`,new.`Day_Date`,new.`Cap_time`);
ELSE
UPDATE `total_time` SET `L_Seen`=new.`Cap_time` WHERE `SSN`=new.`SSN` and `Day_Date`=new.`Day_Date`;
end if;
end
DB-Fiddle:
create table occurance_time(SSN varchar(50),Name varchar(50),Day_Date date,Cap_time time);
CREATE TRIGGER Update_Status AFTER INSERT ON occurance_time
FOR EACH ROW BEGIN
IF NOT EXISTS (SELECT F_Seen FROM total_time WHERE SSN=new.SSN and Day_Date=new.Day_Date)
THEN
INSERT INTO total_time (SSN,Name,Day_Date,F_Seen) VALUES(new.SSN,new.Name,new.Day_Date,new.Cap_time);
ELSE
UPDATE total_time SET L_Seen=new.Cap_time WHERE SSN=new.SSN and Day_Date=new.Day_Date;
end if;
end
db<>fiddle here
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;//
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'));
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 ;
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 ;