Mysql trigger if statement update second table - mysql

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

Related

Command FOR EACH ROW in 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 ;

Getting 1064 error while creating mysql trigger

I am trying to create mysql trigger but getting an error
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 14
Here is my sql script
delimiter $$
CREATE TRIGGER `update_account_balance_after_transfer` AFTER INSERT ON `balance_transfer`
FOR EACH ROW
BEGIN
IF(NEW.from_account_type='CUSTOMER') THEN
UPDATE customer c SET c.balance = c.balance-NEW.amount WHERE c.customer_id= NEW.from_account_id;
ELSE IF(NEW.from_account_type='Vendor') THEN
UPDATE vendor V SET v.balance = v.balance-NEW.amount WHERE v.vendor= NEW.from_account_id;
END IF;
IF(NEW.to_account_type='VENDOR') THEN
UPDATE customer c SET c.balance = c.balance+NEW.amount WHERE c.customer_id= NEW.to_account_id;
ELSE IF(NEW.to_account_type='Vendor') THEN
UPDATE vendor V SET v.balance = v.balance+NEW.amount WHERE v.vendor= NEW.to_account_id;
END IF;
END $$
delimiter ;
Remove space between ELSE and IF. It should be ELSEIF as below.
delimiter $$
CREATE TRIGGER `update_account_balance_after_transfer` AFTER INSERT ON `balance_transfer`
FOR EACH ROW
BEGIN
IF(NEW.from_account_type='CUSTOMER') THEN
UPDATE customer c SET c.balance = c.balance-NEW.amount WHERE c.customer_id= NEW.from_account_id;
ELSEIF(NEW.from_account_type='Vendor') THEN
UPDATE vendor V SET v.balance = v.balance-NEW.amount WHERE v.vendor= NEW.from_account_id;
END IF;
IF(NEW.to_account_type='VENDOR') THEN
UPDATE customer c SET c.balance = c.balance+NEW.amount WHERE c.customer_id= NEW.to_account_id;
ELSEIF(NEW.to_account_type='Vendor') THEN
UPDATE vendor V SET v.balance = v.balance+NEW.amount WHERE v.vendor= NEW.to_account_id;
END IF;
END $$
delimiter ;
MySQL IF Syntax
Check the demo here

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

update Trigger not working

DELIMITER $$
CREATE
/*[DEFINER = { user | CURRENT_USER }]*/
TRIGGER `ir_db1407`.`after_update_ind_user` AFTER UPDATE
ON `ir_db1407`.`ind_user`
FOR EACH ROW BEGIN
SET #user_id=OLD.id
IF (NEW.STATUS = '2' OR NEW.STATUS ='3')
THEN
DELETE FROM ind_user_events WHERE user_id = #user_id;
DELETE FROM ind_user_mobile WHERE user_id = #user_id;
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 'if (new.status = '2' or new.status ='3')
then
delete from ind_user_events wh' at line 7
Change
SET #user_id=OLD.id
to
SET #user_id=OLD.id;
^
You actually don't even need this variable. So ditch that line and try
...
IF (NEW.STATUS = '2' OR NEW.STATUS ='3') THEN
DELETE FROM ind_user_events WHERE user_id = OLD.id;
DELETE FROM ind_user_mobile WHERE user_id = OLD.id;
END IF;
...

trigger update on column change

Hello currently my trigger updates on table update, and I need to change this to only fire when specific column changes.
code:
create trigger money after update on things
for each row
begin
UPDATE `current` c
INNER JOIN things t ON c.id2 = t.id2
INNER JOIN dude_base d ON d.id1 = c.is1
SET c.`curr_cash` = t.thing_cost * d.salary / 100;
end;
$$
And I need to change so it will turn on, when there is update on "thing_cost" from things.
#edit
I have to update that curr_cash and things cost are totaly different values, I cannot compare them, they will always be different.
When I used
if NEW.things_cost <> OLD.things_cost
I get following 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 10
#edit
create trigger wys_sk_b after update on `dude_base`
for each row
begin
if NEW.salary <> OLD.salary
then
UPDATE `current` s
INNER JOIN things u ON s.id_thing = u.id_thing
INNER JOIN dude_base b ON b.id = s.id
SET s.`curr_cash` = u.thing_cost * b.salary/ 100;
end if;
$$
Try this code...
**create table sales** (orderno INT, sale INT,empsalary int, ts TIMESTAMP);
**create table history** (updated varchar(20), oldvalue INT,newvalue INT);
INSERT INTO **sales** (orderno,sale,empsalary) VALUES(1,700,7000);
INSERT INTO **sales** (orderno,sale,empsalary) VALUES(2,800,8000);
INSERT INTO **sales** (orderno,sale,empsalary) VALUES(3,900,9000);
DROP TRIGGER test.instrigger;
DELIMITER ///
CREATE TRIGGER test.instrigger AFTER UPDATE ON sales
FOR EACH ROW
BEGIN
IF NEW.sale <> OLD.sale THEN
INSERT INTO history (updated, oldvalue, newvalue) VALUES('sale', OLD.sale,NEW.sale);
END IF;
IF NEW.empsalary <> OLD.empsalary THEN
INSERT INTO history (updated, oldvalue, newvalue) VALUES('empsalary', OLD.empsalary,NEW.empsalary);
END IF;
END;
///
DELIMITER ;
delimiter $$
create trigger wys_sk_b after update on `dude_base`
for each row
begin
if NEW.salary <> OLD.salary
then
UPDATE `current` s
INNER JOIN things u ON s.id_thing = u.id_thing
INNER JOIN dude_base b ON b.id = s.id
SET s.`curr_cash` = u.thing_cost * b.salary/ 100;
end if;
end;
$$