What is the best way to update multiple tables using a Trigger?

Asked

Viewed 67 times

2

I have a Trigger for After Insert notes After delete notes After update notes

And I need to update several tables like, sales, sales_seller.

I tested using a cursor for each table, at first it worked normally, my doubt and as to the performance and if there is another way to implement.

Follows implemented code:

/**********************************************************************************************/
DROP TRIGGER depoisInsertNotas;

DELIMITER $$
CREATE
TRIGGER `depoisInsertNotas` AFTER INSERT ON `notas` 
FOR EACH ROW BEGIN
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE datan INT DEFAULT 0;
DECLARE totaln DECIMAL(18,4);

DECLARE codven INT;
DECLARE nomeven VARCHAR(150);
DECLARE vendas CURSOR FOR 
SELECT DATE_FORMAT(notas.datreg, '%Y%m') AS anomes, SUM(notas.totnot) AS total 
FROM notas 
WHERE DATE_FORMAT(notas.datreg, '%Y%m') = DATE_FORMAT(NEW.datreg, '%Y%m') 
GROUP BY anomes;

DECLARE vendas_vendedor CURSOR FOR 
SELECT DATE_FORMAT(notas.datreg, '%Y%m') AS anomes, SUM(notas.totnot) AS total, notas.codven, notas.nomven 
FROM notas 
WHERE notas.codven = NEW.codven AND DATE_FORMAT(notas.datreg, '%Y%m') = DATE_FORMAT(NEW.datreg, '%Y%m')
GROUP BY notas.codven, DATE_FORMAT(notas.datreg,'%Y%m');

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := TRUE;
OPEN vendas;

done: LOOP 
  FETCH vendas INTO datan, totaln;

  IF done THEN
     LEAVE done;
  END IF;

  CALL deletaNotas(datan, NULL);
  INSERT INTO vendas(anomes, totven) VALUES (datan, totaln);

END LOOP;
CLOSE vendas;

/*********************************************/

SET done = 0;
OPEN vendas_vendedor;

done: LOOP 
  FETCH vendas_vendedor INTO datan, totaln, codven, nomeven;

  IF done THEN
     LEAVE done;
  END IF;

  CALL deletaNotas(NULL, codven);
  INSERT INTO vendas_vendedor(anomes, totven, codven, nomeven) VALUES (datan, totaln, codven, nomeven);

END LOOP;
CLOSE vendas_vendedor;
END;
$$
DELIMITER ;
  • Is there an example of the operation that was done to trigger these changes? And also how the data would look after the changes? It is also interesting to post your attempt

  • It depends on what your goal is. Enter your code so we can evaluate it better.

  • I edited the question, with the code implemented

1 answer

3


Wribeiro,

The use of cursor is very slow, it is not recommended to use, by itself using TRIGGER is already complicated due to later maintenance, if it is not well documented it is very easy to forget and end up generating future bugs.

To resolve the situation you can use SELECT with INSERT. example:

INSERT INTO banco.tabela-destino (campo1, campo2, campo3...)
SELECT campo1,campo2,campo3... FROM banco.tabela-origem;

In your scenario it would be:

INSERT INTO vendas(anomes, totven)
SELECT DATE_FORMAT(notas.datreg, '%Y%m'), SUM(notas.totnot)
FROM notas 
WHERE DATE_FORMAT(notas.datreg, '%Y%m') = DATE_FORMAT(NEW.datreg, '%Y%m') 
GROUP BY anomes;

INSERT INTO vendas_vendedor(anomes, totven, codven, nomeven)
SELECT DATE_FORMAT(notas.datreg, '%Y%m'), SUM(notas.totnot), notas.codven, notas.nomven 
FROM notas 
WHERE notas.codven = NEW.codven AND DATE_FORMAT(notas.datreg, '%Y%m') = DATE_FORMAT(NEW.datreg, '%Y%m')
GROUP BY notas.codven, DATE_FORMAT(notas.datreg,'%Y%m');
  • I would not use cursor, but I would still need to use a loop to fill the data?

  • 1

    No, because it is being used of data already existent that brought in SELECT, the result of SELECT will be all inserted, if return 5 lines will be new 5 lines inserted.

  • Got it, that way it worked too, this way serves for Update and Delete?

  • 3

    for delete I was able to use it on WHERE and UPDATE I was able to use it on Join, example: UPDATE VEN SET VEN.Value = ??? FROM sales VEN, this way Voce can do the update you want. In case you need help, I can add more example to Voce or help Voce to mount an update

  • How can I make Voce help me with a Trigger in update? edit my question?

  • 1

    I believe that Voce needs to ask a new question and give me the link that I help you.

  • the following link is a new question, https://answall.com/questions/319629/update-tabelas-usando-trigger-a-partir-de-umarre

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.