MySQL Trigger with IF statement returning #1064 - mysql

I am trying to create a trigger which modifies the field api_key_modified in the table wp_users if an entry in the table wp_bp_xprofile_data with the field_id 106 got modified. The entry should only be updated for the same user.
This is my code:
CREATE TRIGGER trigger_api_key_update AFTER UPDATE ON wp_bp_xprofile_data
FOR EACH ROW
IF new.field_id = 106 THEN
UPDATE wp_users SET api_key_modified = 1 WHERE id = new.user_id;
END IF;
And as a result I get:
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
The code does work if i delete the IF statement - but then it will update the data for all modifications not just to the ones with field_id 106.

You need to change the delimiter. Otherwise the definition ends at the first ; which would make it incomplete
delimiter |
CREATE TRIGGER trigger_api_key_update AFTER UPDATE ON wp_bp_xprofile_data
FOR EACH ROW BEGIN
IF new.field_id = 106 THEN
UPDATE wp_users SET api_key_modified = 1 WHERE id = new.user_id;
END IF;
END
|
delimiter ;
Without the if the code works because it is a single statement with only one ; at the end.

Related

Running the following query from my PhpMyadmin SQL tab

I'm running the following query from my PhpMyadmin SQL tab:
CREATE TRIGGER trg_bansach ON bill AFTER INSERT AS
BEGIN
UPDATE addbook
SET Quality = Quality - (
SELECT QualitySale
FROM inserted
WHERE book_id = addbook.book_id
)
FROM addbook
JOIN inserted ON addbook.book_id = inserted.book_id
END
But everytime I'm getting this error msg:
#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 'FROM addbook
WHERE addbook.book_id = inserted.book_id
END' at line 10
Use delimiter
DELIMITER //
CREATE TRIGGER trg_bansach ON bill AFTER INSERT AS
BEGIN
UPDATE addbook
SET Quality = Quality - (
SELECT QualitySale
FROM inserted
WHERE book_id = addbook.book_id
)
FROM addbook
JOIN inserted ON addbook.book_id = inserted.book_id;
END
//
DELIMITER ;

MYSQL Trigger to update different table based on inserted value

I need to update oc_product_attribute with a value obtained from an inserted row in vehicles. How can I get this specific value? (xxxCOLUMN_VALUExxx)
CREATE TRIGGER vehicle_attributes AFTER INSERT ON vehicles
FOR EACH ROW
BEGIN
UPDATE oc_product_attribute SET oc_product_attribute.vehicle_name =
(SELECT CONCAT(vehicles.make_name, ' ', vehicles.model_name, ' ',
vehicles.chassis) FROM vehicles WHERE vehicles.id = xxxCOLUMN_VALUExxx)
WHERE oc_product_attribute.id = xxxCOLUMN_VALUExxx
END
Even simplified below: I still get an error in MYSQL:
CREATE TRIGGER vehicle_attributes AFTER INSERT ON vehicles
FOR EACH ROW
BEGIN
UPDATE oc_product_attribute
SET oc_product_attribute.vehicle_name =
CONCAT(NEW.make_name,' ',NEW.model_name,' ',NEW.chassis);
END;
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
And Simplified further:
CREATE TRIGGER vehicle_attributes AFTER INSERT ON vehicles
FOR EACH ROW
BEGIN
UPDATE oc_product_attribute
SET oc_product_attribute.vehicle_name = 'test' WHERE 1
END;
I still get the same error - Could it be MYSQL doesn't have triggers enabled?
I guess you need to update oc_product_attribute like this:
CREATE TRIGGER vehicle_attributes AFTER INSERT ON vehicles
FOR EACH ROW
BEGIN
UPDATE oc_product_attribute
SET oc_product_attribute.vehicle_name =
CONCAT(NEW.make_name,' ',NEW.model_name,' ',NEW.chassis)
WHERE oc_product_attribute.id = NEW.id;
END
Although I am no sure if oc_product_attribute.id is holding the vehicles.id. However, the important poart is that in an INSERT AFTER TRIGGER you can access the values of the inserted row by NEW.columnName.
N.B. In update triggers you also can access the value before the change with OLD.columnName

MySQL TRIGGER ON INSERT issue

i'm going mad with a MySQL trigger. MySQL says there's an error in the code but i can't figure out what's the problem.
this is the TRIGGER
CREATE TRIGGER UPDATE_COUNTRY
AFTER INSERT
ON `dog`FOR EACH ROW
BEGIN
IF (NEW.export=1)
THEN
IF (NEW.year > (SELECT MAX(`updated_year`) FROM `dog` WHERE `code`=NEW.origin))
THEN
UPDATE `dog` SET `updated_year`= NEW.year, `updated_month`= NEW.month WHERE `code`= NEW.origin;
ELSEIF (NEW.year = (SELECT MAX(`updated_year`) FROM `dog` WHERE `code`=NEW.origine)
AND NEW.month > (SELECT MAX(`updated_month`) FROM `dog` WHERE `code`=NEW.origine AND `updated_year`=NEW.year))
THEN UPDATE `dog` SET `updated_month`=NEW.month WHERE `code`=NEW.origin;
ELSE
RETURN NEW;
END IF;
END IF;
RETURN NEW;
END;
My SQL says
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 13
Thank you all!
Your trigger is:
AFTER INSERT ON `dog`FOR EACH ROW
And your line 13 is
UPDATE `dog` SET `updated_year`= NEW.year, `updated_month`= NEW.month WHERE `code`= NEW.origin;
This kinda suck, but you cannot update/insert/delete with a trigger on the same table that you are currently inserting/updating/deleting data because the table is locked.
This is a limitation from MySQL (yeah and not all SGDB actually have this limitation...(
You will need to work around a different way if you want to update your "dog" table. You could use a store procedure to do both the insert and the update.

What is wrong with the following if statement inside MySql trigger

I'm building a trigger that updates a table when another table get updated. But, for some reason MySql doesn't like the if statement inside it.
The scenario is, there is a table named Group that has level label configuration that are stored in level1, level2, and level3 columns. The other table, Membership, stores member configuration with their level title.
For example if there is a member has level1 title, novice, and there's an update in Group_configuration level1 from 'novice' into 'newbie', trigger will update the member title into 'newbie'. This also applies to level2 and level3.
So my trigger code looks like this:
CREATE TRIGGER update_member_rank_label AFTER UPDATE ON `group`
FOR EACH ROW
begin
if (NEW.level1 <> OLD.level1 ) then
UPDATE membership set level = NEW.level1 where level=OLD.level1 and gid=old.id;
else if (NEW.level2 <> OLD.level2 ) then
UPDATE membership set level = NEW.level2 where level=OLD.level2 and gid=old.id;
else if (NEW.level3 <> OLD.level3 ) then
UPDATE membership set level = NEW.level3 where level=OLD.level3 and gid=old.id
end if;
end;
but I keep getting this 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 5
Any ideas where do I miss?
regards
please check that you set the delimiter to something else than ; before actually creating the trigger:
DELIMITER |
CREATE TRIGGER update_member_rank_label AFTER UPDATE ON `group`
FOR EACH ROW
begin
if (NEW.level1 <> OLD.level1 ) then
UPDATE membership set level = NEW.level1 where level=OLD.level1 and gid=old.id;
else if (NEW.level2 <> OLD.level2 ) then
UPDATE membership set level = NEW.level2 where level=OLD.level2 and gid=old.id;
else if (NEW.level3 <> OLD.level3 ) then
UPDATE membership set level = NEW.level3 where level=OLD.level3 and gid=old.id
end if;
end;
DELIMITER ;
Maybe this helps?
On line 9 add a semicolon. I believe your entire conditional is confused because of this.

MySQL Trigger not saving

Executing this:
CREATE TRIGGER `after_order_insert`
AFTER INSERT ON `hb_orders` FOR EACH ROW
BEGIN
UPDATE hb_accounts
SET hb_accounts.domain = (SELECT companyname FROM hb_client_details
WHERE hb_client_details.id = NEW.client_id
LIMIT 1)
WHERE hb_accounts.client_id = NEW.client_id;
END
Results in this:
/* SQL 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 8 */
What am I missing? This should work, shouldn't it?
Thanks
You're most likely trying to add the trigger without changing the delimiter. Since the trigger contains semicolon, you'll have to change the delimiter to something else temporarily;
DELIMITER //
CREATE TRIGGER `after_order_insert`
AFTER INSERT ON `hb_orders` FOR EACH ROW
BEGIN
UPDATE hb_accounts
SET hb_accounts.domain = (SELECT companyname FROM hb_client_details
WHERE hb_client_details.id = NEW.client_id
LIMIT 1)
WHERE hb_accounts.client_id = NEW.client_id;
END //
DELIMITER ;
An SQLfiddle with the trigger adding successfully. Note that the delimiter is changed in the settings,
Here's another implementation:
CREATE TRIGGER `after_order_insert`
AFTER INSERT ON `hb_orders` FOR EACH ROW
UPDATE hb_accounts a
join hb_client_details b on a.client_id = b.id and b.id = new.client_id
set a.domain = b.companyname;