I have two tables stock and order and i am trying to get the trigger to work so that after the an order has been put in the order quantity is then taken away from the stock quantity field.
DELIMITER $$
CREATE TRIGGER stock_update
AFTER INSERT ON order
FOR EACH ROW
BEGIN
SET #quantity = NEW.quantity
UPDATE stock s;
SET s.quantity = OLD.quantity - NEW.quantity
FROM stock s, order o
WHERE s.ID_stock = o.ID_stock;
END;
$$
DELIMITER;
but i am just getting the error number 1064
There are several issues:
order is a reserved word, therefore you need to use back ticks around it
you don't need to use a variable #quantity
update statement was wrong
DELIMITER and ; should be separated with a space
Try this
DELIMITER $$
CREATE TRIGGER stock_update
AFTER INSERT ON `order`
FOR EACH ROW
BEGIN
UPDATE stock s
SET s.quantity = s.quantity - NEW.quantity
WHERE s.ID_stock = NEW.ID_stock;
END $$
DELIMITER ;
I'm not quite sure it will work, since I would need the table definitions in order to test it.
In each case you should remove the FROM clause and also the last line DELIMITER;. You
should also end the line SET #quantity = NEW.quantity whith a semicolon.
DELIMITER $$
CREATE TRIGGER stock_update
AFTER INSERT ON order FOR EACH ROW
BEGIN
UPDATE stock s;
SET s.quantity = OLD.quantity - NEW.quantity
WHERE s.ID_stock = NEW.ID_stock;
END;
$$
Related
I am trying to create a trigger that will multiply the sales_price field by -1, the sales table is like this transaction type, it has varchar R for refund and S for Sales.
This is the trigger I am trying to create:
delimiter $$
CREATE trigger refund_negative before insert on sale
for each row
begin
set new.SALE_PRICE = new.SALE_PRICE *(-1)
where TRA_TYPE = 'R';
end;
delimiter $$
Try this one:
CREATE TRIGGER `refund_negative` BEFORE INSERT ON `sales` FOR EACH ROW BEGIN
IF NEW.TRA_TYPE='R' THEN
SET NEW.price = NEW.price * -1;
END IF;
END
so basically it just test whether TRA_TYPE is equal to 'R' and if so, NEW.price is multiplied by negative one.
I'm trying to change a value conditionally on my trigger, but I've got an error.
My trigger is:
CREATE TRIGGER `defineBase` BEFORE INSERT ON `perguntas`
FOR EACH ROW
BEGIN
IF NEW.per_base = 0 THEN
SET NEW.per_base = (SELECT per_id FROM perguntas ORDER BY per_id DESC LIMIT 1) + 1;
END IF;
END;
but doesn't work.
You need to change the delimiter to something else than ;. Otherwise the trigger definition stops at the first ;
delimiter |
CREATE TRIGGER `defineBase` BEFORE INSERT ON `perguntas`
FOR EACH ROW
BEGIN
IF NEW.per_base = 0 THEN
SET NEW.per_base = (SELECT per_id FROM perguntas ORDER BY per_id DESC LIMIT 1) + 1;
END IF;
END
|
delimiter ;
I also have the same problem. My SQL code before is just like this (without delimiter):
CREATE TRIGGER update_created_time BEFORE INSERT
ON contact FOR EACH ROW
BEGIN
SET NEW.created=NOW();
END
Then, after i add the following DELIMITER // and close it with the same //
DELIMITER //
CREATE TRIGGER update_created_time BEFORE INSERT
ON contact FOR EACH ROW
BEGIN
SET NEW.created=NOW();
END //
It works. I hope it can help someone in the future...
my problem is as follows:
Table A contains list of "Tasks"
Table B contains "TaskProgress" - just an information about time spent on task, user ID, task ID and note
On table A (Tasks), I have trigger, which updates datetime of last change - column DateChanged (intended to capture datetime of edits made by user)
On table B (TaskProgress) I have trigger, which updates total time spent on a task (sum all times for given Task_ID and update column TotalTime in table A)
I wish to update DateChanged in Table A only when user mades the update (which is every time except when trigger on table B updates TotalTime)
So I wonder, whether there is a way how to tell the database not to fire TRIGGER when updating the values in another trigger.
/* set date of last change and date od closing of task */
DELIMITER $$
CREATE TRIGGER before_tasks_update
BEFORE UPDATE ON tasks
FOR EACH ROW BEGIN
SET new.DateChanged = NOW();
IF new.Status_ID = 3 THEN
SET new.DateClosed = NOW();
END IF;
END$$
DELIMITER ;
/* set total time spent on task */
DELIMITER $$
CREATE TRIGGER after_taskprogress_insert
AFTER INSERT ON taskprogress
FOR EACH ROW BEGIN
DECLARE sumtime TIME;
SET #sumtime := (SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( `timeSpent` ) ) )
FROM taskprogress WHERE Task_ID = new.Task_ID);
UPDATE `tasks` SET TimeReal = #sumtime WHERE ID = new.Task_ID;
END $$
DELIMITER ;
I use MySQL 5.5.40
Thanks, zbynek
Add a check to verify that the update implies a new TimeReal value,since only the second trigger updates that column.
DELIMITER $$
CREATE TRIGGER before_tasks_update
BEFORE UPDATE ON tasks
FOR EACH ROW BEGIN
IF new.TimeReal=old.TimeReal THEN SET new.DateChanged = NOW();
END IF;
IF new.TimeReal=old.TimeReal AND new.Status_ID = 3 THEN
SET new.DateClosed = NOW();
END IF;
END$$
DELIMITER ;
I have two tables product and specific_price
product (id_product,id_supplier,id_manufacturer,quantity)
specific_price (id_specific_price,id_product,id_shop,id_country)
I want to make a mysql trigger.
The AIM on update or insert :
When the 'quantity' of product table is <= 0 get the id_product then
Delete the line of the table specific_price with the same id_product
CREATE TRIGGER control_on_sale_with_stock AFTER UPDATE ON product
FOR EACH ROW
BEGIN
IF NEW.quantity <'1' THEN
DELETE FROM specific_price WHERE id_product=NEW.id_product
END IF;
END;
DELIMITER //
CREATE TRIGGER UPDATE_control_on_sale_with_stock AFTER UPDATE ON product
FOR EACH ROW
BEGIN
IF NEW.quantity <'1' THEN
DELETE FROM specific_price WHERE id_product=NEW.id_product;
END IF;
END;
//
DELIMITER //
CREATE TRIGGER INSERT_control_on_sale_with_stock AFTER INSERT ON product
FOR EACH ROW
BEGIN
IF NEW.quantity <'1' THEN
DELETE FROM specific_price WHERE id_product=NEW.id_product;
END IF;
END;
//
In mysql we need to configure delimiter which was a little annoying because I dind't know that.
And apparently in mysql we can't insert and update in the same trigger (I'm not sure about it).
I have a problem with a MySQL Trigger.
I have 3 tables Customers, Products and Sales.
In Sales I reference customer and product and I want to update the some counts on Products and Customers after a new sale is inserted.
The following trigger fails to update both tables... I cannot figure out what I am doing wrong.
DELIMITER $
CREATE TRIGGER OnSalesInsert AFTER INSERT ON Sales
FOR EACH ROW BEGIN
UPDATE Products SET Products.sold=Products.sold+NEW.amount WHERE Products.id=NEW.product_id;
UPDATE Customers SET Customers.amount=Customers.amount+NEW.amount WHERE Customers.id=NEW.customer_id;
END $
DELIMITER ;
Try this:
DELIMITER $$
CREATE
/*!50017 DEFINER = 'root'#'%' */
TRIGGER `OnSalesInsert` BEFORE INSERT ON `Sales`
FOR EACH ROW BEGIN
UPDATE Products
SET sold = sold + new.amount
WHERE id = new.product_id;
UPDATE Customers
SET amount = amount + new.amount
WHERE id = new.customer_id;
END;
$$
DELIMITER ;
DELIMITER $$
create trigger UpdateAvail after insert on product_details
for each row
Begin
Declare a1 INT;
Declare d1 VARCHAR(1);
Declare d2 VARCHAR(100);
Select count(0) INTO a1 from prod_available where P_Id=new.P_Id;
Select P_Name,P_Brand INTO d1,d2 from product where P_Id=new.P_Id;
IF a1>0 THEN
Update prod_available set P_quantity=P_quantity+new.quantity where P_Id=new.P_Id;
ELSE
insert into prod_available (P_Id,P_Name,P_Brand,P_quantity) values (new.P_Id,d1,d2,new.quantity);
END IF;
END;
$$
DELIMITER ;