I want to make a trigger to do this:
UPDATE formulario
SET ano = EXTRACT(year FROM create_date),
mes = EXTRACT(month FROM create_date)
But i am getting a lot of trouble making it. it is accepted but doesn't work or gives away error message on posting new rows.
I want to insert the create_date
and get the ano
and mes
auto fitted. how can i do this ?
sorry here is the trigger defenition:
CREATE TRIGGER `anomes` AFTER INSERT ON `formulario` FOR EACH ROW BEGIN
UPDATE formulario SET ano = EXTRACT(year FROM create_date);
UPDATE formulario SET mes = EXTRACT(month FROM create_date);
END;
and table:
-- Dumping structure for table at.formulario
CREATE TABLE IF NOT EXISTS `formulario` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`uniqid` varchar(38) DEFAULT NULL,
`create_date` date DEFAULT NULL,
`ano` int(4) DEFAULT NULL,
`mes` int(2) DEFAULT NULL,
`codcliente` int(8) DEFAULT NULL,
`tipo_act` int(1) DEFAULT NULL,
`contacto_nome` varchar(50) DEFAULT NULL,
`contacto_funcao` int(1) DEFAULT NULL,
`contacto_telefone` varchar(20) DEFAULT NULL,
`cara_cli_abc` int(1) DEFAULT NULL,
`cara_cli_estado` int(1) DEFAULT NULL,
`cara_cli_tipo_est` int(3) DEFAULT NULL,
`cara_cli_sazonal` int(1) DEFAULT '0',
`loc_inst_piso` varchar(5) DEFAULT NULL,
`loc_inst_acesso` int(1) DEFAULT NULL,
`loc_inst_data_vistoria` date DEFAULT NULL,
`loc_inst_data_inter` date DEFAULT NULL,
`loc_inst_time_vistoria_start` time DEFAULT NULL,
`loc_inst_time_vistoria_end` time DEFAULT NULL,
`loc_inst_time_inter_start` time DEFAULT NULL,
`loc_inst_time_inter_end` time DEFAULT NULL,
`check_list_p_agua` int(1) DEFAULT '0',
`check_list_t_agua` int(1) DEFAULT '0',
`check_list_p_dist` varchar(3) DEFAULT NULL,
`check_list_obs` text,
`final_obs` text,
`vendedor` int(4) DEFAULT NULL,
`dt_aceito` int(1) DEFAULT NULL,
`bonus` text,
`anulado` tinyint(1) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The problem is that you cannot modify the table that invoked this trigger. It is a restriction.
Try to use a BEFORE INSERT trigger and set new values before the inserting data. For example:
And the question - why are you going to store this data in the table? You can calculate it on the fly in SELECT query.