我是触发器的新手。如果我提出任何愚蠢的问题,请原谅。我试图编写一个触发器,它将更新sol_erp_2014_admission_academic_course_master表中的字段值(名称),并更改sol_erp_2014_academic_course_master表。在下面,我已经提到了表格的结构和我写的触发器。
sol_erp_2014_academic_course_master 表的结构:
CREATE TABLE IF NOT EXISTS `sol_erp_2014_academic_course_master` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'cbcs_major_id',
`major_type` int(11) NOT NULL,
`section_id` int(11) NOT NULL,
`academic_institute_id` int(11) NOT NULL,
`academic_department_id` int(11) NOT NULL,
`specialization_id` varchar(255) NOT NULL DEFAULT '0',
`code` varchar(200) NOT NULL,
`name` varchar(200) NOT NULL,
`unit` int(11) NOT NULL,
`no_of_year` int(11) NOT NULL,
`max_num_of_year` int(11) NOT NULL,
`no_of_sem` int(11) NOT NULL,
`tot_min_credit` int(11) NOT NULL,
`tot_max_credit` int(11) NOT NULL,
`min_sem_pass_prctng` decimal(4,2) NOT NULL,
`routine_system` int(11) NOT NULL COMMENT '1->Day System; 2-> Week System',
`exam_evalution_grade_master_id` int(11) NOT NULL,
`status` enum('0','1') NOT NULL DEFAULT '1',
`date_added` date NOT NULL,
`date_edited` date NOT NULL,
`is_deleted` int(2) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='MAJOR' AUTO_INCREMENT=1 ;
sol_erp_2014_admission_academic_course_master 表的结构:
CREATE TABLE IF NOT EXISTS `sol_erp_2014_admission_academic_course_master` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`academic_institute_id` int(11) NOT NULL,
`academic_department_id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`status` enum('0','1') NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
现在触发我所写的内容:
DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `erp_adbu`.`sol_erp_2014_academic_course_master_update_before`$$
CREATE
/*!50017 DEFINER = 'root'@'%' */
TRIGGER `sol_erp_2014_academic_course_master_update_before` BEFORE UPDATE ON `sol_erp_2014_academic_course_master`
FOR EACH ROW BEGIN
DECLARE name_var VARCHAR(255);
DECLARE id_var INT;
SELECT name
INTO name_var
FROM sol_erp_2014_academic_course_master
WHERE sol_erp_2014_academic_course_master.id = NEW.id;
UPDATE sol_erp_2014_admission_academic_course_master SET name = name_var WHERE id = NEW.id;
END;
$$
DELIMITER ;
现在我正在更新 sol_erp_2014_academic_course_master 表中的记录。但 sol_erp_2014_admission_academic_course_master 表的相应记录未更新。
有人可以帮助我吗?
提前致谢。
答案 0 :(得分:0)
现有触发器存在两个问题:
尝试这样的事情:
DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `erp_adbu`.`sol_erp_2014_academic_course_master_update_before`$$
CREATE
/*!50017 DEFINER = 'root'@'%' */
TRIGGER `sol_erp_2014_academic_course_master_update_before` AFTER UPDATE ON `sol_erp_2014_academic_course_master`
FOR EACH ROW BEGIN
UPDATE sol_erp_2014_admission_academic_course_master SET name = NEW.name WHERE id = NEW.id;
END;
$$
DELIMITER ;