When MySQL cursor is used, the last row is read repeatedly.
Later, we found that we should put the read data in the next loop to avoid duplication.
The Code is as follows:
DELIMITER $
USE 'test' $
Drop procedure if exists 'SP _ getalltablename' $
Create procedure 'SP _ getalltablename' (OUT strAllTableNames VARCHAR (10000 ))
BEGIN
DECLARE bEnd boolean default false;
DECLARE tbName VARCHAR (255 );
# Declare a cursor
DECLARE curTableNames cursor for select TABLE_NAME FROM information_schema.tables AS t WHERE t. table_schema = "test ";
# DECLARE CONTINUE HANDLER
Declare continue handler for sqlstate '000000' SET bEnd = TRUE;
SET strAllTableNames = "";
# Open a cursor
OPEN curTableNames;
# Retrieving data from all rows
# Obtain the first line of content
FETCH curTableNames INTO tbName;
# Loop start
REPEAT
SET strAllTableNames = CONCAT (strAllTableNames, tbName ,";");
# Obtain the content of the next row
FETCH curTableNames INTO tbName;
# Loop end
UNTIL bEnd end repeat;
# Closing a cursor
CLOSE curTableNames;
# Processed
SET strAllTableNames = CONCAT ("All table names:", strAllTableNames );
END $
DELIMITER;
CALL test. sp_getAllTableName (@ allNames );
SELECT @ allNames;