Syntax error on creating trigger for concat two columns value - mysql

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

Related

How can i add trigger event in 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.

Create trigger in mysql on insert where it compares fields of added row

I am new with mysql triggers, I have 2 tables in a database, one is called tasks and the other is task_rules.
Once a new task_rule is inserted, I want to compare the field time (which is a time object) to the current time.
if it is greater than the current time, I want to add a new row in tasks and set rid (in tasks) to id of the newly added rule, and the time field in tasks to the time field of the newly added row.
I am getting many syntax errors and i didnt know how to create this trigger.
BEGIN
DECLARE #time TIME
DECLARE #freq VARCHAR(400)
#time = NEW.time
#freq = NEW.frequency
IF (#time > NOW()) AND (#freq == 'daily') THEN
INSERT INTO task_rules ('rid', 'time') VALUES (NEW.id, #time)
END IF
END
Im doing it using phpmyadmin
1) user defined variable (those preceded with #) should not be declared see How to declare a variable in MySQL? 2) to assign a value to a variable you have to use the SET statement 3) every statement must be terminated - if you are using phpmyadmin and the default terminator is set to ; change it and terminate your statements in the trigger with ; see - https://dev.mysql.com/doc/refman/8.0/en/stored-programs-defining.html 4) null safe equals in mysql is not == from memory this should be <=> see https://dev.mysql.com/doc/refman/8.0/en/comparison-operators.html 5) you should probably set delimiters before and after the trigger 6) column names should be escaped with back ticks not single quotes. 7) for each row clause missing before begin statement.
try this
drop trigger if exists t;
delimiter $$
create trigger t after insert on task
for each row
BEGIN
DECLARE vtime TIME;
DECLARE vfreq VARCHAR(400);
set time = NEW.time;
set freq = NEW.frequency;
IF (vtime > NOW()) AND (vfreq <=> 'daily') THEN
INSERT INTO task_rules (`rid`, `time`) VALUES (NEW.id, vtime);
END IF;
END $$
delimiter ;
And do review https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html

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

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

Error 1111: mysql on trigger from insert

I am trying to understand why this trigger keeps giving me an error about invalid use of grouped function when i try to run a basin insert statement to test this out.
I have tried working with this to figure out what i am doing wrong but the error just remains the same. Error 1111
DROP TRIGGER a_num;
DELIMITER //
CREATE TRIGGER a_num BEFORE INSERT ON test_a
FOR EACH ROW BEGIN
DECLARE last INT DEFAULT 0;
INSERT INTO test_b SET full_name = CONCAT_WS(' ', NEW.f_name, NEW.l_name);
SET last = COUNT(id);
UPDATE test_b SET number = CONCAT_WS('-', last, LEFT(NEW.f_name, 2), LEFT(NEW.f_name, 2)) WHERE id = last;
END;
//
Please don't mind the use or poor construction I quite a newb.
Thanks.
I think it should be -
DROP TRIGGER a_num;
DELIMITER //
CREATE TRIGGER a_num BEFORE INSERT ON test_a
FOR EACH ROW BEGIN
DECLARE last INT DEFAULT 0;
INSERT INTO test_b SET full_name = CONCAT_WS(' ', NEW.f_name, NEW.l_name);
SET last = LAST_INSERT_ID();
UPDATE test_b SET number = CONCAT_WS('-', last, LEFT(NEW.f_name, 2), LEFT(NEW.f_name, 2)) WHERE id = last;
END;
//
Can you provide the CREATE statement for test_a and the INSERT statement you're using?
In MySQL Workbench if you right click on test_a go to Copy to Clipboard..Create Statement, will send the table definition.
Is there a reason you're inserting and then updating the same record? Could you combine this into one insert?