问题
This is my trigger.
I want to make trigger on 1 table (pelayanan).
CREATE DEFINER=`root`@`localhost` TRIGGER `before_insert_pelayanan`
BEFORE INSERT ON `pelayanan` FOR EACH ROW
IF NEW.`ESTIMASI` IS NULL OR NEW.`ESTIMASI` = 0 AND
`dbhpl`.`pelayanan`.`DAYA` <= 5500
THEN SET NEW.ESTIMASI = 4;
ELSEIF NEW.ESTIMASI IS NULL OR NEW.ESTIMASI = 0 AND
`dbhpl`.`pelayanan`.`DAYA` <= 33000 AND
`dbhpl`.`pelayanan`.`DAYA` >= 6600
THEN SET NEW.ESTIMASI = 15;
ELSEIF NEW.ESTIMASI IS NULL OR NEW.ESTIMASI = 0 AND
`dbhpl`.`pelayanan`.`DAYA` <= 197000 AND
`dbhpl`.`pelayanan`.`DAYA` >= 41500
THEN SET NEW.ESTIMASI = 40;
ELSE SET NEW.ESTIMASI = 100;
END IF
When I execute the trigger, it has been created. But, when I insert data into table pelayanan, It become
#1109 - Unknown table 'pelayanan' in field list.
How can I resolve this?
I have to remove dbhpl.pelayanan.DAYA
become pelayanan.DAYA
and DAYA
. But, It doesn't work.
回答1:
use NEW before column name. This is the right code-
mysql>
CREATE DEFINER=`root`@`localhost` TRIGGER `before_insert_pelayanan`
BEFORE INSERT ON `pelayanan` FOR EACH ROW
IF NEW.`ESTIMASI` IS NULL OR NEW.`ESTIMASI` = 0 AND
`NEW`.`DAYA` <= 5500
THEN SET NEW.ESTIMASI = 4;
ELSEIF NEW.ESTIMASI IS NULL OR NEW.ESTIMASI = 0 AND
`NEW`.`DAYA` <= 33000 AND
`NEW`.`DAYA` >= 6600
THEN SET NEW.ESTIMASI = 15;
ELSEIF NEW.ESTIMASI IS NULL OR NEW.ESTIMASI = 0 AND
`NEW`.`DAYA` <= 197000 AND
`NEW`.`DAYA` >= 41500
THEN SET NEW.ESTIMASI = 40;
ELSE SET NEW.ESTIMASI = 100;
END IF
Query OK, 0 rows affected (0.11 sec)
mysql> insert into pelayanan values ();
Query OK, 1 row affected (0.08 sec)
mysql> select * from pelayanan ;
+----------+------+
| ESTIMASI | daya |
+----------+------+
| 4 | NULL |
+----------+------+
1 row in set (0.00 sec)
来源:https://stackoverflow.com/questions/31178162/error-1109-unknown-table-in-field-list-caused-by-trigger