How can i add trigger event in mysql? - mysql

I have trigger statement but its not working
CREATE TRIGGER tg_table3_insert BEFORE INSERT ON rd_stonepanel_pricing
FOR EACH ROW BEGIN
INSERT INTO table3_seq VALUES (NULL);
SET NEW.option_id = CONCAT('R', LPAD(LAST_INSERT_ID(), 3, '0'));
But it shows an error like 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 3

I am assuming you are trying to update a sequence when an insert is done. See this(http://en.latindevelopers.com/ivancp/2012/custom-auto-increment-values/).
The below code snippet may work.
CREATE TRIGGER tg_table3_insert BEFORE INSERT ON rd_stonepanel_pricing
FOR EACH ROW
BEGIN
INSERT INTO table3_seq SET option_id = CONCAT('R', LPAD(LAST_INSERT_ID(), 3, '0');
END;
Remove the semicolon at line 3 if it is a single statement and add end. Also, the above code will only work if the table "table3_seq" has one column.

Related

MySQL syntax error while creating a UPDATE Trigger

I am trying to create a trigger on updating the salary field of this table.
customers(ID, Name, Age, Address, Salary) from this site SQL Trigger
While creating it shows the following error
MySQL said:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'OR INSERT OR UPDATE ON customers
FOR EACH ROW
WHEN (NEW.ID > 0)
DECLARE
' at line 2
Here is the code snippet:
BEFORE DELETE OR INSERT OR UPDATE ON customers
FOR EACH ROW
WHEN (NEW.ID > 0)
DECLARE
sal_diff number;
BEGIN `enter code here`
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary difference: ' || sal_diff);
END
Note that, I'm using phpMyAdmin. Does dbms_ouput.put_line() works there?
As said in the comments, you are using Oracle syntax, it can't work on MySQL.
Then, there's no reason to have this trigger on DELETE and INSERT because your aim is to compute the salary difference on a specific row before and after it get updated. You can't have a value NEW on DELETE, and you can't have a value OLD on INSERT.
Thus your computation is only meaningful on UPDATE.
Here's the correct MySQL syntax (I am assuming that you have a column sal_diff on your table)
DELIMITER $$
CREATE TRIGGER `update_sal_diff`
BEFORE UPDATE ON `customers `
FOR EACH ROW
BEGIN
SET NEW.sal_diff = NEW.salary - OLD.salary;
END $$
DELIMITER ;
If this is not what you are trying to achieve, edit your question and add clear requirements

Syntax error on creating trigger for concat two columns value

I'm trying to create a trigger by concat two fields
CREATE TRIGGER format_goods_sn
BEFORE INSERT ON tp_goods
FOR EACH ROW
BEGIN
SET NEW.goods_sn = CONCAT('TP', LPAD(NEW.goods_id, 7, '0'))
END
but I got alert said SET NEW.goods_sn = CONCAT('TP', LPAD(NEW.goods_id, 7, '0')) has syntax error?
I found that just use single line format (remove 'BEGIN' and 'END') would resolve this issue:
CREATE TRIGGER format_goods_sn
BEFORE INSERT ON tp_goods
FOR EACH ROW
BEGIN
SET NEW.goods_sn = CONCAT('TP', LPAD(NEW.goods_id, 7, '0'))

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.

Cannot create trigger in mysql at phpmyadmin

I'm trying to create an ID by combining fields after a row has been inserted into the database in phpmyadmin. I get an error on line 3 of the following trigger statement:
DROP TRIGGER IF EXISTS `scanID2`;
CREATE DEFINER=`root`#`localhost`
TRIGGER `scanID2`
AFTER INSERT ON db_name.`scan_data`
FOR EACH ROW
BEGIN
UPDATE db_name.scan_data
SET #data1 = concat(RIGHT(scanContent,2), RIGHT(operatorID,2), RIGHT(deviceID,2), RIGHT (scanDate, '-', ''), REPLACE (scanTime, ':', ''));
INSERT INTO db_name.scan_data(scanID) VALUES (#data1);
END
//
The error is:
#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 'CREATE DEFINER=`root`#`localhost`
TRIGGER `scanID2`
AFTER INSERT ON db_name' at line 2
OK, so I figured how to get the query executed. The trigger has been created but I don't think it does what it's supposed to do. The records don't get updated. Here's the new code:
BEGIN
SET #data1 = concat(RIGHT(scanContent,2), RIGHT(operatorID,2), RIGHT(deviceID,2), REPLACE (scanDate, '-', ''), REPLACE (scanTime, ':', ''));
INSERT INTO vacseen1_connect.scan_data(scanID) VALUES (#data1);
END
There are several things to consider:
Use BEFORE event for the trigger instead of AFTER. This way you can set the value of scanID the same time a row is being inserted.
Use NEW keyword to access fields of a row being inserted.
Since it's one statement trigger now you don't need to change delimiter and use BEGIN ... END block.
You most likely meant to use REPLACE for scanDate.
Try
CREATE TRIGGER scanID2
BEFORE INSERT ON scan_data
FOR EACH ROW
SET NEW.scanID = CONCAT(
RIGHT(NEW.scanContent, 2),
RIGHT(NEW.operatorID, 2),
RIGHT(NEW.deviceID, 2),
REPLACE(NEW.scanDate, '-', ''),
REPLACE (NEW.scanTime, ':', '')
);
Here is a SQLFiddle demo