Command FOR EACH ROW in mysql - mysql

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 ;

Related

Update Trigger on same table MySQL 5.6

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);

Mysql trigger if statement update second table

I am trying to create a mysql trigger. This is what I am trying to create, but somehow it tells me that: "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 'A
FOR EACH ROW".
What Am I doing wrong here? I have tried to naming them differently, but it does not seem to work.
delimiter //
CREATE TRIGGER stock_update BEFORE UPDATE ON `stock` A
FOR EACH ROW
BEGIN
IF NEW.`quantity` < 1 THEN //belongs to alias A
UPDATE `ps_product` B
SET B.`visibility` = 'none';
WHERE id_product = OLD.id_product //Should be the id_product of A alias
ELSEIF NEW.quantity > 0 THEN
UPDATE `ps_product` C
SET C.`visibility` = 'both';
WHERE id_product = OLD.id_product //Should be the id_product of A alias
END IF;
END;//
delimiter ;
Try this one
delimiter //
CREATE TRIGGER stock_update BEFORE UPDATE ON `stock`
FOR EACH ROW
BEGIN
IF NEW.`quantity` < 1 THEN #belongs to alias A
UPDATE `ps_product` B
SET B.`visibility` = 'none'
WHERE id_product = OLD.id_product; #Should be the id_product of A alias
ELSEIF NEW.quantity > 0 THEN
UPDATE `ps_product` C
SET C.`visibility` = 'both'
WHERE id_product = OLD.id_product; #//Should be the id_product of A alias
END IF;
END;//
delimiter ;
This shows no errors in mysql workbbench

Error when run query trigger in MySQL

please help
i have trigger code below
DELIMITER $$
CREATE TRIGGER `mydb`.`table0`
AFTER INSERT ON `mydb`.`table0`
FOR EACH ROW
BEGIN
IF (
SELECT table1.idtable1 FROM table2, table1, table
WHERE table1.idtable1=table2.idtable2
and table0.idtable0=table1.idtable1
)
THEN
UPDATE targettable
SET targettable.column = 1
WHERE targettable.idtable=table1.idtable1;
END IF;
END$$
after running it, it shows 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 '' at line 17
how to fix it? MySQL version is 5.5.34
table is a reserved word in mysql. Surround table with backticks. Also you were missing delimiter and a semicolon at the end.
This works:
DELIMITER $$
CREATE TRIGGER `mydb`.`table0`
AFTER INSERT ON `mydb`.`table0`
FOR EACH ROW
BEGIN
IF (
SELECT table1.idtable1 FROM table2, table1, `table`
WHERE table1.idtable1=table2.idtable2
and table0.idtable0=table1.idtable1
)
THEN
UPDATE targettable
SET targettable.column = 1
WHERE targettable.idtable=table1.idtable1;
END IF;
END;$$
delimiter ;
I hope that helps!
DELIMITER $$
CREATE TRIGGER `mydb`.`table0`
AFTER INSERT ON `mydb`.`table0`
FOR EACH ROW
BEGIN
IF (
SELECT table1.idtable1 FROM table2, table1, table <-- you should escape this using backticks as this is a reserved word i.e., `table`
WHERE table1.idtable1=table2.idtable2
and table0.idtable0=table1.idtable1
)
THEN
UPDATE targettable
SET targettable.column = 1
WHERE targettable.idtable=table1.idtable1;
END IF;
END$$

Trigger creation query failed

I have three tables, when data is inserted into third table, I am checked whether newly inserted row has field value in table1. If value exists then I am moving the same row from table3 to table2. My code is like below, But I am getting error while executing this
DELIMITER $$
CREATE TRIGGER onRosterAdd
AFTER INSERT ON yet_tobe
FOR EACH ROW
BEGIN
SELECT CASE WHEN (SELECT 1 FROM users WHERE NEW.jid = (users.username + "_1")) > 0
THEN
INSERT INTO rusers SELECT * FROM yet_tobe WHERE yet_tobe.id = NEW.id;
DELETE FROM yet_tobe where yet_tobe.id = NEW.id;
END
END$$
DELIMITER ;
And here is the error
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 'INTO rusers SELECT * FROM yet_tobe WHERE yet_tobe.id = NEW.id;
How can I fix this.
Try it like this:
DELIMITER $$
CREATE TRIGGER onRosterAdd
BEFORE INSERT ON yet_tobe
FOR EACH ROW
BEGIN
IF (SELECT 1 FROM users WHERE CONCAT(username,"_1")=NEW.jid)>0
THEN
INSERT INTO rusers SELECT * FROM yet_tobe WHERE yet_tobe.id = NEW.id;
DELETE FROM yet_tobe where yet_tobe.id = NEW.id;
END IF;
END$$
DELIMITER ;
Here's a working fiddle

MySQL Trigger on 2 tables with

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