Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql Trigger Loop for query result with many rows

hi i have a database with many tables and foreign keys like this

CREATE TABLE IF NOT EXISTS `articulos` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nombre` varchar(63) NOT NULL,
  `contenido` text NOT NULL,
  `normas_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=138 ;

CREATE TABLE IF NOT EXISTS `aspectosambientales` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nombre` varchar(63) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=28 ;

CREATE TABLE IF NOT EXISTS `aspectosambientales_articulos` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `aspectosambientales_id` int(11) NOT NULL,
  `articulos_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_aspaspectosambientales1`      (`aspectosambientales_id`),
  KEY `fk_aspee` (`articulos_id`) 
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 UTO_INCREMENT=225 ;

CREATE TABLE IF NOT EXISTS `empresas` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `razonsocial` varchar(127) DEFAULT NULL,
  `nit` varchar(63) DEFAULT NULL,
  `direccion` varchar(127) DEFAULT NULL,
  `telefono` varchar(15) DEFAULT NULL,
  `web` varchar(63) DEFAULT NULL,
  `auth_user_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

CREATE TABLE IF NOT EXISTS `articulos_empresas` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `empresas_id` int(11) NOT NULL,
  `articulo_id` int(11) NOT NULL,
  `acciones` text,
  `responsable` varchar(255) DEFAULT NULL,
  `plazo` date DEFAULT NULL,
  `cumplido` tinyint(1) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_normas_empresas_empresas1` (`empresas_id`),
  KEY `fk_normas_empresas_normas1` (`normas_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

and i need to create a trigger to fill the 'articulos_empresas' after insert in 'empresas' for all rows in 'articulos' that match with 'aspectosambientals' that the new 'empresas' selected.

I get all 'articulos' with this query

SELECT articulos_id FROM aspectosambientales_articulos 
    WHERE  aspectosambientales_id = ID 
        -- ID is the aspectosambientales_id selected when the 'empresas' row is created
        --  maybe something like NEW.aspectosambientales_id

but i dont know how create a loop like ' for loop' in trigger for every result in the query

some like this:

CREATE TRIGGER 'filltableae' AFTER INSERT ON 'empresas' 
FOR EACH ROW
BEGIN 
DECLARE arrayresult = (SELECT articulos_id FROM aspectosambientales_articulos 
    WHERE  aspectosambientales_id = NEW.aspectosambientales_id)
--- here is when i have to do the loop for all the results
--- for ids in arrayresults
---  insert into articulos_empresas ('',NEW.id, ids, '', '' ,'','')
--- endfor
END

thanks!!!

like image 922
elin3t Avatar asked Aug 13 '12 22:08

elin3t


People also ask

How can I loop through all rows of a table in MySQL?

MySQL loop through rows and INSERT This section will create a cursor to fetch each row from one table and insert the data into another table. Let us start by looking into the data to be used in the example. Assume that we have a table student_enroll_date with the below rows. 19 row(s) returned.

What is trigger How many triggers are possible in MySQL?

A MySQL trigger is a stored program (with queries) which is executed automatically to respond to a specific event such as insertion, updation or deletion occurring in a table. There are 6 different types of triggers in MySQL: 1.

Which trigger is execute for each row?

If the triggering event does not process any rows, a FOR EACH ROW triggered action does not execute. For a trigger on a table, if the triggering event is a SELECT statement, the trigger is a called a Select trigger, and the triggered actions execute after all processing on the retrieved row is complete.

Can we create more than one trigger of same event on same table?

You can create multiple triggers for the same subject table, event, and activation time. The order in which those triggers are activated is the order in which the triggers were created.


2 Answers

Based on @Razvan answer i left here the code for the trigger, so maybe can help somebody

DROP TRIGGER IF EXISTS AEINST;
DELIMITER //
CREATE TRIGGER AEINST AFTER INSERT ON procesos_aspectos
FOR EACH ROW
BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE ids INT;
    DECLARE cur CURSOR FOR SELECT articulos_id FROM aspectosambientales_articulos WHERE aspectosambientales_id = NEW.aspectosambientales_id;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    OPEN cur;
        ins_loop: LOOP
            FETCH cur INTO ids;
            IF done THEN
                LEAVE ins_loop;
            END IF;
            INSERT INTO articulos_empresas VALUES (null,ids, NEW.empresas_id,null,null,null,null);
        END LOOP;
    CLOSE cur;
END; //
DELIMITER ;

thanks again!

like image 133
elin3t Avatar answered Sep 17 '22 10:09

elin3t


As far as I know you can iterate through the result of a SELECT query using cursors. See here : http://dev.mysql.com/doc/refman/5.0/en/cursors.html

like image 27
Razvan Avatar answered Sep 17 '22 10:09

Razvan