Connection between circular variables and characters in Stored Procedures of common tables to be merged

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.