2013-09-27 83 views
0

我有一個存儲過程。該函數生成表和觸發器,因爲表已成功創建,但直到現在觸發器未成功創建:|。MYSQL:如何從存儲過程創建觸發器

源代碼:

DELIMITER $$ 

CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_inventarisasi`(IN _id_bbws INT, IN _nama_lokasi VARCHAR(100), IN _id_das INT, IN _ws VARCHAR(100), IN _id_provinsi INT, IN _id_kota_kab INT, IN _lat FLOAT(10,6), IN _lng FLOAT(10,6), IN _tahun_bangun INT, IN _id_tipe INT, IN _id_merk INT, IN _nama_pengamat VARCHAR(256)) 
BEGIN 
    DECLARE txn_error INTEGER DEFAULT 0; 
    DECLARE CONTINUE HANDLER FOR SQLEXCEPTION BEGIN 
       SET txn_error = 1; 
    END; 

    SET @tipealat = (SELECT nama_tipe FROM t_tipe WHERE id_tipe = _id_tipe); 
    SET @icon = 'default'; 

    IF UPPER(@tipealat) LIKE CONCAT(UPPER('PDA'), '%') THEN 
     SET @icon = 'pda'; 
    ELSEIF UPPER(@tipealat) LIKE CONCAT(UPPER('PCH'), '%') THEN 
     SET @icon = 'pch'; 
    END IF; 

    SAVEPOINT savepoint_pointer; 
    INSERT INTO `dhtml`.`t_inventarisasi` 
    (`id_bbws`,`nama_lokasi`,`id_das`,`ws`,`id_provinsi`,`id_kota_kab`,`lat`,`lng`,`tahun_bangun`, `id_tipe`,`id_merk`,`nama_pengamat`, `icon`) 
    VALUES 
    (_id_bbws, _nama_lokasi, _id_das, _ws, _id_provinsi, _id_kota_kab, _lat, _lng, _tahun_bangun, _id_tipe, _id_merk, _nama_pengamat, @icon); 
    IF txn_error = 1 THEN 
     ROLLBACK TO savepoint_pointer; 
     SELECT '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">&times;</button><strong>Gagal!</strong> Input Data !</div>' AS hasil; 
    ELSE 
     SELECT '<div class="alert alert-success"><button type="button" class="close" data-dismiss="alert">&times;</button><strong>Sukses!</strong> Input Data !</div>' AS hasil; 
     COMMIT; 

     SET @sql = CONCAT('CREATE TABLE `lokasi`.`',_nama_lokasi,'` (
                    `ReceivedDate` date NOT NULL, 
                    `ReceivedTime` time NOT NULL, 
                    `SamplingDate` date NOT NULL, 
                    `SamplingTime` time NOT NULL, 
                    `Rain` int(11) DEFAULT NULL, 
                    `WLevel` int(11) DEFAULT NULL, 
                    PRIMARY KEY (`ReceivedDate`,`ReceivedTime`,`SamplingDate`,`SamplingTime`) 
                  ) '); 
     PREPARE stmt FROM @sql; 
     EXECUTE stmt; 
     DEALLOCATE PREPARE stmt; 

     SET @trigger = CONCAT('DELIMITER $$ 
           USE `lokasi`$$ 
           CREATE 
           DEFINER=`root`@`localhost` 
TRIGGER `lokasi`.`onINSERT_',_nama_lokasi,'` 
AFTER INSERT ON `lokasi`.`',_nama_lokasi,'` 
FOR EACH ROW 
BEGIN 
    IF NEW.WLevel > 0 THEN 
    SET @icon = \'pda\'; 
    SET @id_tipe = (SELECT id_tipe FROM `dhtml`.`t_tipe` WHERE UPPER(nama_tipe) LIKE UPPER(\'PDA%\') LIMIT 1); 
    SET @id_inventarisasi = (SELECT id_inventarisasi FROM `dhtml`.`t_inventarisasi` WHERE UPPER(nama_lokasi) = UPPER(\'',_nama_lokasi,'\') LIMIT 1); 
    IF NEW.ReceivedDate = CURDATE() THEN 
     IF NEW.WLevel > 500 && NEW.WLevel < 1500 THEN 
      SET @icon = \'normal\'; 
     ELSEIF NEW.WLevel > 1500 && NEW.WLevel < 2500 THEN 
      SET @icon = \'sedang\'; 
     ELSEIF NEW.WLevel > 2500 THEN 
      SET @icon = \'awas\'; 
     END IF;    
    ELSE 
     SET @10hari = (SELECT CURDATE() - INTERVAL 10 DAY); 
     IF NEW.ReceivedDate <= @10hari THEN 
      SET @icon = \'mati\'; 
     END IF;   

     SET @5hari = (SELECT CURDATE() - INTERVAL 5 DAY); 
     IF NEW.ReceivedDate > @10hari AND NEW.ReceivedDate <= @5hari THEN 
      SET @icon = \'tdkupdate\'; 
     END IF; 
    END IF; 
     UPDATE `dhtml`.`t_inventarisasi` SET icon = @icon WHERE id_inventarisasi = @id_inventarisasi AND id_tipe = @id_tipe; 
    END IF; 

END$$ 
'); 
     PREPARE stmt FROM @trigger; 
     EXECUTE stmt; 
     DEALLOCATE PREPARE stmt; 
    END IF; 

END 

有一些提示?

+0

是否引發錯誤?如果是,請將這些詳細信息添加到您的帖子。 –

回答