MySQL Create trigger for get value from another column - mysql

i have a table which contains columns as "barkoda1,barkoda2,barkoda3,#pdfa1,#pdfa2,#pdfa3", what i'm trying to do is when a record assigned to barkoda1 copy same record to #pdfa1,for example if barkoda1 defined as "1234567890" #pdfa1 should be "1234567890.pdf".
i tried to create a trigger for it, but failed;
DELIMITER //
CREATE TRIGGER pdfdeneme AFTER INSERT
ON finyeb_ba_2369_bio_for_2__r_d_
FOR EACH ROW
BEGIN
UPDATE finyeb_ba_2369_bio_for_2__r_d_ SET `#pdfa1`=NEW.barkoda1;
END//
DELIMITER ;
when i create this trigger,it doesn't return any error but when i add record to table mysql returns "it is already used by statement which invoked this stored function/trigger".
also tried this way
DELIMITER //
CREATE TRIGGER pdfdeneme BEFORE INSERT
ON finyeb_ba_2369_bio_for_2__r_d_
FOR EACH ROW
BEGIN
INSERT INTO finyeb_ba_2369_bio_for_2__r_d_(`#pdfa1`) VALUES(NEW.barkoda1);
END//
DELIMITER ;
But same error happened, what am i doing wrong here?

You need a BEFORE INSERT TRIGGER
And then only use the SET Clause
DELIMITER //
CREATE TRIGGER pdfdeneme BEFORE INSERT
ON finyeb_ba_2369_bio_for_2__r_d_
FOR EACH ROW
BEGIN
SET NEW.`#pdfa1` = CONCAT(NEW.barkoda1,'.pdf');
END//
DELIMITER ;
But your column name is a bad choice, it leads on the first glance to false conclusions.

Related

function not found when i try to update the mother of trigger

I have a problem with MySQL trigger. When I tried to update a database which is the mother of the trigger it says function not found
DELIMITER $$
CREATE TRIGGER trg_barang BEFORE UPDATE ON barang FOR EACH ROW BEGIN
INSERT INTO log_barang
set id_barang = OLD.id_barang,
harga_barang_baru=new.harga_barang,
harga_barang_lama=Old.harga_barang();
END
$$
DELIMITER ;
this trigger will show the data from "barang" and will store to this new trigger.and then when I try to update, it says function "old.harga_barang not found" how do I fix it?

Error trigger insert into after insert

Hi guys I am trying to make some triggers to update in one table after insert in other table but it reports me an error. Two tables have the same fields and with the same name.
Here is the code.
DELIMITER //
CREATE TRIGGER Insertar_Bares_Private AFTER INSERT ON Bares
FOR EACH ROW
BEGIN
INSERT INTO Bares_Private (nombrebar, direccion) VALUES (new.nombrebar, new.direccion);
END//
CREATE TRIGGER Insertar_Categorias_Private AFTER INSERT ON Categorias
FOR EACH ROW
BEGIN
INSERT INTO Categorias_Private (nombrecategoria) VALUES (new.nombrecategoria);
END//
CREATE TRIGGER Insertar_Productos_Private AFTER INSERT ON Productos
FOR EACH ROW
BEGIN
INSERT INTO Productos_Private (nombreproducto, idcategoria, descripcion, precio, imagen) VALUES (new.nombreproducto, new.idcategoria, new.descripcion, new.precio, new.imagen);
END//
-- Triggers al actualizar
CREATE TRIGGER Actualizar_Usuarios_Private AFTER UPDATE on Usuarios
for each ROW
BEGIN
UPDATE Usuarios_Private
SET nombreusuario=new.nombreusuario, contrasenia=new.contrasenia, email=new.email, telefono=new.telefono
where idusuario=new.idusuario;
END//
CREATE TRIGGER Actualizar_Bares_Private AFTER UPDATE on Bares
for each ROW
BEGIN
UPDATE Bares_Private
SET nombrebar=new.nombrebar, direccion=new.direccion
where idbar=new.idbar;
END//
CREATE TRIGGER Actualizar_Categorias_Private AFTER UPDATE on Categorias
for each ROW
BEGIN
UPDATE Categorias_Private
SET nombrecategoria=new.nombrecategoria
where idcategoria=new.idcategoria;
END//
CREATE TRIGGER Actualizar_Productos_Private AFTER UPDATE on Productos
for each ROW
BEGIN
UPDATE Productos_Private
SET nombreproducto=new.nombreproducto, idcategoria=new.idcategoria, descripcion=new.descripcion, precio=new.precio, imagen=new.imagen
where idproducto=new.idproducto;
END//
DELIMITER ;
And here is the error reported.
#1235 - This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table'
Thank you in advice.
Remove the ; from all the END; and apply the delimiter END// will solve your problem.
Since you already placed the proper ; for the INSERT/UPDATE statement, so no need to place it again in the END;.
You only need to change the delimiter before defining a trigger if the trigger contains more than one query. This is because you need to use the default delimiter ; in this case to separate the queries within the trigger, but the MySQL client will incorrectly think you mean to terminate the trigger definition here. This also requires you to use BEGIN ... END around the block of queries in the trigger.
DELIMITER //
CREATE TRIGGER Insertar_Usuarios_Private AFTER INSERT ON Usuarios
FOR EACH ROW
BEGIN
INSERT INTO Usuarios_Private (nombreusuario, contrasenia, email,telefono) VALUES (new.nombreusuario, new.contrasenia, new.email, new.telefono);
END//
CREATE TRIGGER Insertar_Bares_Private AFTER INSERT ON Bares
FOR EACH ROW
BEGIN
INSERT INTO Bares_Private (nombrebar, direccion) VALUES (new.nombrebar, new.direccion);
END//
-- and so on ...
DELIMITER ;
However, your triggers each only contain one query. Therefore you can dispense with the delimiter and the BEGIN ... END block entirely.
CREATE TRIGGER Insertar_Usuarios_Private AFTER INSERT ON Usuarios
FOR EACH ROW
INSERT INTO Usuarios_Private (nombreusuario, contrasenia, email,telefono) VALUES (new.nombreusuario, new.contrasenia, new.email, new.telefono);
CREATE TRIGGER Insertar_Bares_Private AFTER INSERT ON Bares
FOR EACH ROW
INSERT INTO Bares_Private (nombrebar, direccion) VALUES (new.nombrebar, new.direccion);
-- and so on ...

How do I set the NEW trigger values using a stored procedure?

How do I set the NEW trigger values using a stored procedure? For instance, BEFORE INSERT ON myTable, I want to set the NEW value of myTable.someColumn to a given value. When trying to do so, I get a 1193 error when trying to create the procedure. Thank you
DELIMITER $$
CREATE PROCEDURE `myProc` ()
BEGIN
//How do I set NEW.someColumn of myTable?
SET NEW.someColumn=123;
END$$
CREATE TRIGGER myTrig BEFORE INSERT ON myTable FOR EACH ROW
BEGIN
//SET NEW.someColumn=123; //This works, but I want to do the setting within the stored procedure
CALL myProc();
END$$
DELIMITER ;
INSERT INTO myTable(id,data) VALUES (1,"hello");
//Above "should" really do INSERT INTO myTable(id,data,someColumn) VALUES (1,"hello",123);
1193 - Unknown system variable 'someColumn'
EDIT. The following will work.
DELIMITER $$
CREATE PROCEDURE `myProc` (INOUT myID int)
BEGIN
SET myID =123;
END$$
CREATE TRIGGER myTrig BEFORE INSERT ON myTable FOR EACH ROW
BEGIN
CALL myProc(NEW.someColumn);
END$$
DELIMITER ;
You can not do it exactly the way you started. But you might be able to achieve what you are looking for by having the stored procedure just give you a value, and then use this value within the trigger:
delimiter //
CREATE PROCEDURE myProc (OUT param1 INT)
BEGIN
SET #value = 123;
END//
delimiter ;
Then you can have your trigger call the stored procedure and use it's value:
CREATE TRIGGER myTrig BEFORE INSERT ON myTable FOR EACH ROW
BEGIN
CALL myProc(#columnValue);
SET NEW.someColumn = #columnValue;
END$$
I haven't tested it, just typing straight out of my head. If it doesn't work, please let me know, and I will revise my answer.
Also, let me know, if this is not what you were looking for, or not complete enough, and I will revise my answer.
(p.s. I have no idea why somebody thought this is not a good question.)

Trouble in creating Trigger in MySQL

I have a database with some tables. I want to create a trigger to 'Delete a record in tblplayersfield with "pID" of the record which was deleted in tblplayers'
CREATE TRIGGER delete_from AFTER DELETE on tblplayers
FOR EACH ROW
BEGIN
DELETE FROM tblplayerfields
WHERE 'tblplayerfields'.'pID' = OLD.'pID';
END
You need to add a delimiter change first
delimiter |
CREATE TRIGGER delete_from AFTER DELETE on tblplayers
FOR EACH ROW
BEGIN
DELETE FROM tblplayerfields
WHERE 'tblplayerfields'.'pID' = OLD.'pID';
END
|
delimiter ;
The delimiter signals the DB engine the end of your statement. Normally it is ;. But that would end the stored procedure at the first ;. And its definition would be incomplete.
You can change the delimiter and add it to the end of your procedure. After that change the delimiter back to ;

MySQL Trigger On A Column

I hav a table with several fields. One field is "date_assigned" and the other is "assigned". "date_assigned" has a datatype of timestamp and can be null. "assigned" has a datatype of tinyint and the values are either 0 (default; 'not assigned') or 1 ('assigned').
I would like to create a trigger that would automatically update the "assigned" value to 1 when "date_assigned" is updated with a value (is not null).
I've used triggers before, but have not used them in conjunction with checking if a value is null. I'm unclear on the syntax, so any help would be appreciated. So far, I've tried:
DELIMITER $$
CREATE
TRIGGER `<database>`.`<trigger_name>` AFTER UPDATE
ON `<database>`.`<table>`
FOR EACH ROW BEGIN
IF(NEW.date_assigned IS NOT NULL) THEN
UPDATE <table> SET assigned = '1';
END$$
DELIMITER ;
I just get Error Code: 1064. I looked upo the code, and it appears that it's a syntax error. So what syntax mistake am I making, and is this even the correct 'grammar'?
Try putting BEGIN in a new line as follows.
DELIMITER $$
CREATE
TRIGGER `<database>`.`<trigger_name>` AFTER UPDATE
ON `<database>`.`<table>`
FOR EACH ROW
BEGIN
IF(NEW.date_assigned IS NOT NULL) THEN
UPDATE <table> SET assigned = '1';
END; //Change here also.
$$
DELIMITER ;
This is because of default delimiter in MySQL set to ;. So, the first line should look like DELIMITER $$;
DELIMITER $$;
CREATE TRIGGER `<database>`.`<trigger_name>`
AFTER UPDATE ON `<database>`.`<table>`
FOR EACH ROW BEGIN
IF(NEW.date_assigned IS NOT NULL) THEN
UPDATE <table> SET assigned = '1';
END$$
DELIMITER ;