2012-08-13 3 views
7
とクエリ結果の

こんにちは、私はこのMySQLのトリガーループ多数の行

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 ; 

のような多くのテーブルと外部キーを使用してデータベースを持っていると私はempresas」の挿入後に「articulos_empresas」を埋めるためのトリガーを作成する必要があります'' articulos 'のすべての行が' aspectosambientals 'と一致し、新しい' empresas 'が選択されています。

私はこのクエリ

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 

とすべての「articulos」を取得するが、私は、クエリのすべての結果のためのトリガーに「forループ」のようなループを作成する方法を知らない

このようないくつか:

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 

ありがとうございます!

答えて

8

私が知る限り、カーソルを使用してSELECTクエリの結果を反復することができます。 こちらをご覧ください:@Razvan回答に基づいてhttp://dev.mysql.com/doc/refman/5.0/en/cursors.html

+0

プロシージャとしてirを保存した場合、他のトリガーで使用する必要がある場合は、プロシージャのパラメータとしてNEW.idを送信できます。 – elin3t

+0

@Razvanの回答に基づいて、ここに結果を残しました: – elin3t

28

を私はので、多分、再び誰か

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 ; 

感謝を助けることができ、ここでトリガー用のコードを残しました!

+0

偉大な答え生産と実践に非常に役立ちます。 –

関連する問題