I have 2 tables
FIRST
ps_product (id_product, price)
SECOND
ps_product_price (id_product, price, price_tax)
Now what I trying to do is to SET TRIGGER:
DELIMITER $$
CREATE TRIGGER `after_ps_product_price_update`
BEFORE UPDATE ON `ps_product_price`
FOR EACH ROW
BEGIN
UPDATE ps_product
SET price = NEW.price_tax -(NEW.price_tax * 0.1666666666666666666)
WHERE id_product = NEW.id_product;
END
When ps_rpoduct_price TABLE, COLUMN(price_tax) is updated then update TABLE ps_product, COLUMN (price)
And that works fine but when I try do do this same other way around:
DELIMITER $$
CREATE TRIGGER `after_ps_product_update`
AFTER UPDATE ON `ps_product`
FOR EACH ROW
BEGIN
UPDATE ps_product_price
SET price_tax = (NEW.price * 1.2)
WHERE id_product = NEW.id_product;
END
Then I get an error:
Can't update table 'ps_rpoduct' in stored function/trigger because it is already used by statement which invoked this stored function/trigger
Related
i want to move my current data to another table when the update value is "jakarta"
IF NEW.alamat = "jakarta" THEN
INSERT INTO detail_mhs_copy1
SELECT * FROM detail_mhs WHERE id_mhs = NEW.id_mhs;
DELETE FROM detail_mhs WHERE id_mhs = OLD.id_mhs;
END IF;
but with this trigger its make error like this
1442 - Can't update table 'detail_mhs' in stored function/trigger because it is already used by statement which invoked this stored function/trigger
any solutions?
Please try below trigger and change col1 & col2 with your column's name and change primary key with column name which have primary key and not updating.
DELIMITER $$
CREATE TRIGGER detail_mhs_trig
AFTER UPDATE ON detail_mhs FOR EACH ROW
BEGIN
IF NEW.alamat = "jakarta" THEN
INSERT INTO detail_mhs_copy1(col1,col2, alamat)
VALUES(old.col1, old.col2, new.alamat);
END IF; END$$
DELIMITER ;
I have a table 'foods' with columns 'featured', 'restaurant_id', 'retaurant_stock'.
How can I update column 'restaurant_stock' using trigger or any other solution available on mySQL version 5.6 after update of column 'featured' on the same table on a condition?
Calculated columns are not available on mySQL version 5.6.
Here's my trigger:
CREATE TRIGGER update_restaurant_stock BEFORE UPDATE ON foods
OR EACH ROW
BEGIN
IF NEW.featured = 1 THEN
UPDATE `foods` SET NEW.restaurant_stock = (NEW.restaurant_id);
ELSE
UPDATE `foods` SET NEW.restaurant_stock = 0;
END IF;
END
Now when I update column 'featured' I get the following error message:
#1442 - Can't update table 'foods' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Thanks for you help!
You can't update the current row that way, you can use set to change the new values
DELIMITER $$
CREATE TRIGGER update_restaurant_stock BEFORE UPDATE ON foods
OR EACH ROW
BEGIN
IF NEW.featured = 1 THEN
SET NEW.restaurant_stock = (NEW.restaurant_id);
ELSE
SET NEW.restaurant_stock = 0;
END IF;
END$$
DELIMITER ;
CREATE TRIGGER tr_bu_foods
BEFORE UPDATE
ON foods
FOR EACH ROW
-- update restaurant_stock
SET NEW.restaurant_stock = NEW.restaurant_id * (NEW.featured = 1);
Help me please.I want to create trigger in mysql server but server wrote me
SQL query: Documentation
CREATE TRIGGER `dis_out_of_stock` AFTER UPDATE ON `ps_stock_available`
FOR EACH ROW
begin
UPDATE `ps_product_shop` SET active=0 WHERE id_product IN (SELECT
id_product FROM `ps_stock_available` WHERE quantity=0);
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 4
Here is my code:
CREATE TRIGGER dis_out_of_stock AFTER UPDATE ON ps_stock_available
FOR EACH ROW
begin
UPDATE ps_product_shop SET active=0 WHERE id_product IN (SELECT id_product FROM ps_stock_available WHERE quantity=0);
UPDATE ps_product_shop SET active=1 WHERE id_product IN (SELECT id_product FROM ps_stock_available WHERE quantity>0);
end
Thanks and have a nice day.
You need to redefine DELIMITER to something else other than ;, for eg: $$
Also, check if the trigger already exists or not, using DROP TRIGGER IF EXISTS.
At the end, redefine the DELIMITER to ;
Try:
DELIMITER $$
DROP TRIGGER IF EXISTS dis_out_of_stock $$
CREATE TRIGGER dis_out_of_stock
AFTER UPDATE ON ps_stock_available
FOR EACH ROW begin
UPDATE ps_product_shop
SET active=0
WHERE id_product IN (SELECT id_product
FROM ps_stock_available
WHERE quantity = 0);
END $$
DELIMITER ;
You should not be attempting to update the entire ps_product_shop table. Instead, just look at the record that was just modified:
DELIMTER $$
CREATE TRIGGER `dis_out_of_stock` AFTER UPDATE ON `ps_stock_available`
FOR EACH ROW
BEGIN
UPDATE ps_product_shop ps
SET active = 0
WHERE ps.id_product = new.id_product and new.quantity = 0;
END;
DELIMITER ;
I have a table called sum_test with id,a,b,c fields
I need a trigger that calculates the sum of a+b on c but Im new to triggers.
How can I do that on Mysql?
CREATE TRIGGER `example` AFTER INSERT ON `sum_test`
FOR EACH ROW BEGIN
UPDATE sum_test
SET c= (a+b)
WHERE id = id
END
If we are wanting to set column c on the row we are inserting, then we can do like this, in a BEFORE insert trigger:
DELIMITER $$
CREATE TRIGGER `sum_test_bi` BEFORE INSERT ON `sum_test`
FOR EACH ROW
BEGIN
SET NEW.c = NEW.a + NEW.b ;
END$$
DELIMITER ;
I don't understand the reference to table suma. What does that table have to do with anything? We note that the condition id = id is going to evaluate to TRUE for every row in the table where id IS NOT NULL.
I am trying to update the same table before inserting new row in the table.
I want to set the status of all previous rows with the same product_id to 0 and after that insert new row with the status 1....Please help. here is my code written mysql..
DROP TRIGGER IF EXISTS `priceStatusUpdate`//
CREATE TRIGGER `priceStatusUpdate` BEFORE INSERT ON `sale_price`
FOR EACH ROW BEGIN
UPDATE sale_price
SET status=0
WHERE product_id=new.product_id;
END
//
Use a stored procedure instead:
DELIMITER $$
CREATE PROCEDURE procedureName (IN p_product_id)
BEGIN
UPDATE sale_price SET status = 0 WHERE product_id = p_product_id;
INSERT INTO sale_price (product_id) VALUES (p_product_id);
END $$
DELIMITER ;
Then call it with
CALL procedureName(1);
DROP TRIGGER IF EXISTS `priceStatusUpdate`//
CREATE TRIGGER `priceStatusUpdate` BEFORE INSERT ON `sale_price`
FOR EACH ROW BEGIN
SET NEW.status= 0;
END