First, let's explain the effect of the stored procedure function used:
Query each table name field in the data source table, query each table according to the table name, count the amount of data in each table, and then update the data source table.
The general situation of the data source table is as follows:
Stored procedure:
DROP PROCEDURE IF EXISTS updateDataSource; CREATE PROCEDURE updateDataSource () BEGIN DECLARE dataSize INT DEFAULT 0; DECLARE tableName VARCHAR (30); DECLARE done INT DEFAULT FALSE; DECLARE tableList CURSOR FOR SELECT data_table FROM data_source; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; OPEN tableList; read_loop : LOOP FETCH tableList INTO tableName; IF done THEN LEAVE read_loop; END IF; SET @STMT := CONCAT( "SELECT count(*) INTO @dataSize", " FROM ", tableName, ";" ); PREPARE STMT FROM @STMT; EXECUTE STMT; DEALLOCATE PREPARE STMT; UPDATE data_source SET DATA_SIZE = @dataSize WHERE DATA_TABLE = tableName; END LOOP ; CLOSE tableList; END
Stored procedure description:
1. Take the value in the cursor in the LOOP loop and obtain each indication;
2. In order to dynamically use the table name in the stored procedure, define a sql statement @STMT, use function splicing, and assign the execution result of the sql statement to a variable for subsequent use;
3. PREPARE STMT FROM @ STMT , preprocessing sql, the scope is visible to the current client connection session; EXECUTE STMT executes the preprocessed sql, if the executed STMT does not exist or is not defined, an error will be reported; DEALLOCATE PREPARE STMT releases the preprocessed sql sql resource;