MySQL:字段列表中的未知表

时间:2014-01-03 03:01:32

标签: mysql sql triggers mysql-5.5

我有3个现有表:transactionstransaction_typesexpenses

$> USE `mydb`;

   Datebase changed.

$> SHOW TABLES;

   expenses
   transactions
   transaction_types

在调用该过程的expenses上添加一个存储过程和两个触发器后,我再也无法插入到表中了:

$> INSERT INTO `expenses` SET `date`='2013-12-22';

   1109. Unknown table 'expenses' in field list

$> INSERT INTO `expenses` (`date`) VALUES('2013-12-22');

   1109. Unknown table 'expenses' in field list

但我可以从精干中选择......

如果我放下触发器,我可以再次插入expenses

我的2个触发器是重复的,1个用于更新,1个用于插入:

USE `mydb`;

DELIMITER $$

DROP TRIGGER IF EXISTS `expense_updated_paid`
CREATE TRIGGER `expense_updated_paid`
    AFTER UPDATE ON `expenses` FOR EACH ROW
    BEGIN
        CALL `expense_paid`( NEW.`id` , NEW.`date paid` , NEW.`amount`);
    END$$

程序:

USE `mydb`;
DROP procedure IF EXISTS `expense_paid`;

DELIMITER $$

CREATE PROCEDURE `expense_paid`(IN `expense_id` INT, IN `date` DATE, IN `amount` INT)
    BEGIN
        IF `expenses`.`date paid` IS NOT NULL THEN
            SET @type_id = (SELECT `id` FROM `transaction_types` WHERE `name` = 'reimbursement');
            INSERT INTO `transactions`
                SET
                    `transactions`.`date` = `date`,
                    `transactions`.`amount` = `amount`,
                    `transactions`.`type_id` = @type_id,
                    `transactions`.`note` = `expense_id`;
        END IF;
    END$$

我希望MySQL在触发器或程序中抱怨某些内容,如果这会导致问题而不是告诉我该表不存在...

1 个答案:

答案 0 :(得分:1)

现在您提供了程序代码,答案很明确:

    IF `expenses`.`date paid` IS NOT NULL THEN

该程序没有expenses.*的上下文。也就是说,当限定符引用过程外的查询时,您不能在过程内使用限定列名。如果您接受列限定符在给定查询中引用相关名称而不是表本身,则更有意义。

但是该过程确实有date输入参数,您将其作为相同值NEW.\ date paid``传递。因此,将行更改为以下内容:

    IF `date` IS NOT NULL THEN