Error #1109 - unknown table in field list caused by trigger

老子叫甜甜 提交于 2021-02-10 12:24:25

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!