1

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

然而,问题在于,在工作流表上使用 process_index 为“process_one”执行插入后,触发器中的 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 ;
4

0 回答 0