1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
DELIMITER $$
DROP TRIGGER t1_INS $$
CREATE TRIGGER ETABUFR_INS AFTER INSERT ON t1 FOR EACH ROW
CALL MAJ_t1_dpt(NEW.Dpt, NEW.Id, 'INSERT') $$
DROP TRIGGER t1_DEL $$
CREATE TRIGGER ETABUFR_DEL BEFORE DELETE ON t1 FOR EACH ROW
CALL MAJ_t1_dpt(OLD.Dpt, OLD.Id, 'DELETE') $$
DROP TRIGGER t1_UPD $$
CREATE TRIGGER ETABUFR_UPD AFTER UPDATE ON t1 FOR EACH ROW
CALL MAJ_t1_dpt(NEW.Dpt, NEW.Id, 'UPDATE') $$
DROP PROCEDURE MAJ_t1_dpt $$
CREATE PROCEDURE MAJ_t1_dept (IN vDept VARCHAR(8), IN id VARCHAR(32), IN typeAction VARCHAR(16))
BEGIN
DECLARE NameTable VARCHAR(30) DEFAULT 't1_';
SET NameTable = CONCAT('t1_', vDept) ;
IF ( typeAction = 'INSERT' ) THEN
-- Début De La Requête D'Insertion
Set @Stmt := Concat('INSERT INTO ', Nametable ,' Select * From t1 Where Id = \'', id ,'\' ;');
-- On Prépare Le Stmt Avec @Stmt Qui Contient Notre Requête
Prepare Stmt From @Stmt;
-- On Exécute Donc La Requête
Execute Stmt;
-- On Supprime Le Stmt
Deallocate Prepare Stmt;
ELSEIF ( typeAction = 'DELETE' ) THEN
-- Début De La Requête De Suppression
Set @Stmt := Concat('DELETE FROM ', Nametable ,' Where Id = \'', id ,'\' ;');
-- On Prépare Le Stmt Avec @Stmt Qui Contient Notre Requête
Prepare Stmt From @Stmt;
-- On Exécute Donc La Requête
Execute Stmt;
-- On Supprime Le Stmt
Deallocate Prepare Stmt;
ELSE
-- Début De La Requête De Suppression
Set @Stmt := Concat('DELETE FROM ', Nametable ,' Where Id = \'', id ,'\' ;');
-- On Prépare Le Stmt Avec @Stmt Qui Contient Notre Requête
Prepare Stmt From @Stmt;
-- On Exécute Donc La Requête
Execute Stmt;
-- On Supprime Le Stmt
Deallocate Prepare Stmt;
-- Début De La Requête D'Insertion
Set @Stmt := Concat('INSERT INTO ', Nametable ,' Select * From t1 Where Id = \'', id ,'\' ;');
-- On Prépare Le Stmt Avec @Stmt Qui Contient Notre Requête
Prepare Stmt From @Stmt;
-- On Exécute Donc La Requête
Execute Stmt;
-- On Supprime Le Stmt
Deallocate Prepare Stmt;
END IF;
END $$
DELIMITER ; |
Partager