I work with a database that has hundreds of tables with the very same structure and an identical syntax. Everyday more tables are created, thus the database is continuously growing. To dynamically append this growing amount of tables I use a MySQL
procedure which works just fine.
Yet once appended I cannot identify the origin of the rows, since the tables don´t contain any information to be distinguished. Hence I would like to integrate a MySQL
statement in the following procedure to automatically get the TABLE_NAME
from the information_schema
and set is as a value in the corresponding table. I already created a column for that, but struggle with the adjustment of the procedure. I would really appreciate any help or suggestion to solve this.
CREATE PROCEDURE getallarbeitsplaene(recordOffset INT, recordCount INT)
BEGIN
DECLARE tableName VARCHAR(255);
DECLARE endOfTables INT DEFAULT 0;
DECLARE cur CURSOR FOR
SELECT t.table_name
FROM information_schema.tables t
WHERE t.table_schema = DATABASE()
AND t.table_type='BASE TABLE'
AND NOT table_name LIKE '%x_arbeitsplan'
AND table_name LIKE '%_arbeitsplan';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET endOfTables = 1;
DROP TEMPORARY TABLE IF EXISTS alle_plaene;
CREATE TEMPORARY TABLE alle_plaene (
`ID` INT(11) NOT NULL,
`AP` VARCHAR(8) NOT NULL,
`Name` VARCHAR(128) NOT NULL,
`Baugruppentyp` VARCHAR(128) NOT NULL,
`Basiseinheit` VARCHAR(8) NOT NULL,
`Zeit_Basiseinheit` NUMERIC(10,0) NOT NULL,
`vor_OF` BINARY(1) NOT NULL,
`zwischen_OF` BINARY(1) NOT NULL,
`nach_OF` BINARY(1) NOT NULL,
`Annahme_Grundlage` VARCHAR(64) NOT NULL,
`Budget_AG_Bankraum` VARCHAR(7) NOT NULL,
`Budget_AG_FU` VARCHAR(7) NOT NULL,
`Datum_voraus` DATE,
`Budget_voraus` INT(11) NOT NULL
) ENGINE=MEMORY;
OPEN cur;
tablesLoop: LOOP
FETCH cur INTO tableName;
IF endOfTables = 1 THEN
LEAVE tablesLoop;
END IF;
SET @s = CONCAT('INSERT INTO alle_plaene (Name, Baugruppentyp, Basiseinheit, Zeit_Basiseinheit, vor_OF, zwischen_OF, nach_OF, Annahme_Grundlage, Budget_AG_Bankraum, Budget_AG_FU, Datum_voraus, Budget_voraus) SELECT `Name`, `Baugruppentyp`, `Basiseinheit`, `Zeit_Basiseinheit`, `vor_OF`, `zwischen_OF`, `nach_OF`, `Annahme_Grundlage`, `Budget_AG_Bankraum`, `Budget_AG_FU`, `Datum_voraus`, `Budget_voraus` FROM ', tableName, ' ORDER BY ID DESC');
PREPARE stmt FROM @s;
EXECUTE stmt;
END LOOP;
CLOSE cur;
SELECT `Name`, `Baugruppentyp`, `Basiseinheit`, `Zeit_Basiseinheit`, `vor_OF`, `zwischen_OF`, `nach_OF`, `Annahme_Grundlage`, `Budget_AG_Bankraum`, `Budget_AG_FU`, `Datum_voraus`, `Budget_voraus` FROM alle_plaene;
END