Sheshman
Sheshman

Reputation: 88

MySQL Create trigger for get value from another column

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?

Upvotes: 1

Views: 479

Answers (1)

nbk
nbk

Reputation: 49375

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.

Upvotes: 1

Related Questions