General table merging Stored PROCEDURE circular variables and character connection Java code mysql> DELIMITER $ mysql> drop PROCEDURE sp_test_switch $ Query OK, 0 rows affected (0.01 sec) mysql> create procedure sp_test_switch ()-> BEGIN-> declare a int;-> declare B varchar (5000);-> set a = 1; -> set B = '';-> while a <10 do-> set B = concat (B, ',', a);-> set a = a + 1; -> end while;-> select B;-> END-> $ Query OK, 0 rows affected (0.00 sec) mysql> DE LIMITER; mysql> call sp_test_switch (); + ---------------------- + | B | + -------------------- + |, 1, 2, 4, 5, 6, 7, 8, 9 | + ------------------ + 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql> mysql Stored Procedure variable definition: what is the difference between declare and direct set @ var? In my understanding, declare defines local variables and can only be used in stored procedures or functions. The effective interval is the begin end interval of the variable declared in stored procedures or functions. @ var is a user variable (self-declared), and its effective range is the session. You can use it in stored procedures or functions, it can also be used in self-written SQL statements. also @ do not need to declare the type, DECLARE must specify the type of Java code create procedure sp_starnight_cbj () begin declare a int default 1; declare B VARCHAR (5000) DEFAULT ''; while a <10 do set B = CONCAT (B, ',', A); set a = A + 1; end while; select B; END; application: java code drop procedure if exists 'merage _ branch_table '; create procedure 'merage _ branch_table' (IN p_table_name VARCHAR (200), IN p_branch_size INT, in p_lpad INT) begin declare m_begin_row int default 0; SET @ m_merage_ SQL _view = CONCAT ('create view', p_table_name, 'as'); WHILE m_begin_row <p_branch_size DO IF (m_begin_row = p_branch_size-1) then set @ m_merage_ SQL _view = CONCAT (@ m_merage_ SQL _view, 'select * from', p_table_name, '_', LPAD (m_begin_row, p_lpad, 0 )); else set @ m_merage_ SQL _view = CONCAT (@ m_merage_ SQL _view, 'select * from', p_table_name, '_', LPAD (m_begin_row, p_lpad, 0), 'Union all'); END IF; SET m_begin_row = m_begin_row + 1; end while; PREPARE main_stmt FROM @ m_merage_ SQL _view; EXECUTE main_stmt; END; Views of all table shards