What is wrong with this trigger? - mysql

I want to set only one row actived.
If somebody try to set the estrutura_versao_status as 1, set every other estrutura_versao_status to 0 before and keep this new row as the only one estrutura_versao_status = 1.
CREATE TRIGGER tgr_classifica_ativa_revisao BEFORE INSERT ON `sys_estrutura`
FOR EACH ROW
BEGIN
IF (NEW.estrutura_versao_status = 1) THEN
UPDATE `sys_estrutura` SET estrutura_versao_status = 0;
END IF;
END;
This 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 '' at line 5

Assuming you want to set the flag for the newly inserted row, just use assign that to the NEW variable. There is no need to do an UPDATE on the table.
CREATE TRIGGER tgr_classifica_ativa_revisao BEFORE INSERT ON sys_estrutura
FOR EACH ROW
BEGIN
IF (NEW.estrutura_versao_status = 1) THEN
SET NEW.estrutura_versao_status = 0;
END IF;
END;

Related

trigger to edit other table in mysql

I have the following trigger:
DELIMITER $$
DROP TRIGGER IF EXISTS trg_day_1_status_update$$
USE `tbl_user_status`$$
CREATE DEFINER = CURRENT_USER TRIGGER `tbl_user_status`.`trg_day_1_status_update` AFTER INSERT ON `tbl_tf_day_1` FOR EACH ROW
BEGIN
UPDATE tbl_user_status
SET NEW.roadmap_day = tbl_tf_day_1.roadmap_day,
SET NEW.user_status = "active",
SET NEW.latest_submit = tbl_tf_day_1.submitted_on,
SET NEW.latest_tf_id = tbl_tf_day_1.tf_id,
SET NEW.d0 = 1,
SET NEW.latest_cig_intake = tbl_tf_day_1.q_id_3_ftnd,
SET NEW.latest_cigintake_submit = tbl_tf_day_1.submitted_on
WHERE id_user = tbl_tf_day_1.id_user LIMIT 1;
END;
$$
DELIMITER ;
But I keep getting an 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 'SET NEW.user_status = "active",
SET NEW.latest_submit = tbl_tf_day_1.submitted' at line 5
I've tried a bunch of stuff, including removing the 'NEW' before the fields, changing the value to be inserted, and even making this trigger BEFORE the insert. Can't figure out what's wrong, please help!
A few things wrong here, set only needs to be stated once, the direction of the set statements is incorrect , the column to the left of the equals (=) should the the column you wish to set in the table you wish to update and the column to the right of the equals (=) should the the column you wish to update it from pre- ceeded by a NEW. qualifier (but not for constants such as 'active', the where clause column name to the right of the equals (=) should be preceeded by NEW. I am also a bit dubious about the table name you are trying to update which seeems to share a name with the db/schema in your create statement.
You only need to use the SET keyword once, then comma-separate the fields, like this:
UPDATE tbl_user_status SET
NEW.roadmap_day = tbl_tf_day_1.roadmap_day,
NEW.user_status = "active",
NEW.latest_submit = tbl_tf_day_1.submitted_on,
NEW.latest_tf_id = tbl_tf_day_1.tf_id,
NEW.d0 = 1,
NEW.latest_cig_intake = tbl_tf_day_1.q_id_3_ftnd,
NEW.latest_cigintake_submit = tbl_tf_day_1.submitted_on
WHERE id_user = tbl_tf_day_1.id_user LIMIT 1;

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

Keep Getting a Syntax Error when Creating MySQL Trigger

I am trying to Create a Trigger that will fire AFTER Insert of a Record where I will see if there is other records similar to this Inserted Record (Same Date) and if so will update a column in the inserted Record. Once I complete this one I will also update it for AFTER Update as well. Any Help would be Greatly Appreciated.
CREATE
TRIGGER `INSERT_POSTDATEINDEX` AFTER INSERT
ON `zoomloca_listings-dev`.`listings_posts`
FOR EACH ROW
BEGIN
DECLARE vNewPostDateIndex INT;
DECLARE vLastPostDateIndex INT DEFAULT '0';
SET vNewPostDateIndex = '0';
SET vLastPostDateIndex = (SELECT POSTDATEINDEX FROM listings_posts WHERE date(POST_DATE) = date(NEW.POST_DATE) ORDER BY POSTDATEINDEX DESC LIMIT 1);
IF vLastPostDateIndex = '0' THEN
SET vNewPostDateIndex = '0';
ELSE
SET vNewPostDateIndex = vLastPostDateIndex + 1;
END IF;
Update `listings_posts` SET POSTDATEINDEX = vNewPostDateIndex where ID = New.ID;
END
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 6
The problem is that there is no TOP in MySQL. You have to use LIMIT instead. Besides, if you are not using mysql client, then you should remove DELIMITER since it is not a feature of the MySQL. Another thing is that to demarcate the end of an IF statement in MySQL, you should use END IF instead of ENDIF.

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.

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;