SELECT在MySQL触发器中使用声明的变量返回没有行

时间:2012-05-11 10:48:01

标签: mysql triggers insert where

我正在尝试执行INSERT ... SELECT,以便在“工作流程”表中创建行后,在“任务”表中创建行。 (工作流创建中使用的process_index查找'process_tasks'表中所需的那些任务,然后在tasks表中创建它们。)

但问题是,在工作流表上执行带有'process_one'的process_index的插入后,触发器中的SELECT找不到任何行。我认为@process_id没有正确设置,但是我在触发器中注释了替代插入,它表明@process_index正确设置。有人可以建议吗?

以下是一些用于演示此问题的简化代码:

DROP TABLE IF EXISTS workflow;
CREATE TABLE workflow (
  id INT(10) PRIMARY KEY AUTO_INCREMENT,
  process_index VARCHAR(12)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS tasks;
CREATE TABLE tasks (
  id INT(10) PRIMARY KEY AUTO_INCREMENT,
  process_index_used VARCHAR(12),
  target_field VARCHAR(12)
) ENGINE=INNODB DEFAULT CHARSET=utf8;


DROP TABLE IF EXISTS process_tasks;
CREATE TABLE process_tasks (
  id INT(10) PRIMARY KEY AUTO_INCREMENT,
  process_index VARCHAR(12),
  source_field VARCHAR(12)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

INSERT INTO process_tasks SET process_index = 'process_one', source_field = 'alpha';
INSERT INTO process_tasks SET process_index = 'process_one', source_field = 'beta';

DROP TRIGGER IF EXISTS workflow_tasks;
DELIMITER //
CREATE TRIGGER workflow_tasks AFTER INSERT ON workflow
FOR EACH ROW BEGIN
  DECLARE process_index VARCHAR(12);
  SET @process_index = NEW.process_index;
--  INSERT INTO tasks (process_index_used) VALUES (@process_index);
  INSERT INTO tasks (target_field) SELECT source_field FROM process_tasks WHERE process_index = @process_index;
END//
DELIMITER ;

0 个答案:

没有答案