I need to create a simple trigger:
DELIMITER //
CREATE TRIGGER `create_stock_articulo` AFTER INSERT ON `almacen_tb_articulos`
FOR EACH ROW
BEGIN
DECLARE cur CURSOR FOR SELECT id FROM 'almacen_tb_almacenes';
DECLARE id INT;
OPEN cur;
REPEAT
FETCH cur INTO id;
INSERT INTO 'almacen_tb_stock' VALUES (id,NEW.id);
UNTIL done END REPEAT;
CLOSE cur;
END;//
This trigger is supposed to add rows on the table 'almacen_tb_stock' when a row is inserted in 'almacen_tb_articulos'. Each added row must contain 'almacen_tb_almacenes.id' and the new 'almacen_tb_articulos.id'.
When I try to create this trigger I get this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''almacen_tb_almacenes'; DECLARE id INT; OPEN cur; REPEAT FETCH cur I' at line 4
I can't see where the error is.
The table definitions are these:
almacen_tb_articulos:
CREATE TABLE IF NOT EXISTS `almacen_tb_articulos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`codigo` varchar(15) NOT NULL,
`descripcion` varchar(150) NOT NULL,
... more irrelevant fields,
PRIMARY KEY (`id`),
KEY `id_familia` (`id_familia`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=43
almacen_tb_almacenes:
CREATE TABLE IF NOT EXISTS `almacen_tb_almacenes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`almacen` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`bloqueado` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
almacen_tb_stock:
CREATE TABLE IF NOT EXISTS `almacen_tb_stock` (
`id_almacen` int(11) NOT NULL,
`id_articulo` int(11) NOT NULL,
`cantidad` int(11) NOT NULL,
PRIMARY KEY (`id_almacen`,`id_articulo`),
KEY `id_almacen` (`id_almacen`),
KEY `id_articulo` (`id_articulo`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
SELECT id FROM 'almacen_tb_almacenes'orSELECT id FROM almacen_tb_almacenes?