我有一个商店程序。此函数生成表和触发器,表已成功创建,但直到现在未成功创建触发器:|。
源代码:
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">×</button><strong>Gagal!</strong> Input Data !</div>' AS hasil;
ELSE
SELECT '<div class="alert alert-success"><button type="button" class="close" data-dismiss="alert">×</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 :(得分:1)
预准备语句目前不支持创建触发器。见13.5. SQL Syntax for Prepared Statements
答案 1 :(得分:0)
选中此项:http://forums.mysql.com/read.php?98,588793,588793#msg-588793。 1.无法从准备好的声明中创建触发器 2.无法从存储过程创建触发器 因此,拆分代码,您可以将创建触发器的部分放在PHP代码中。