MySQL string segmentation and dynamic Execution stitching SQL

Source: Internet
Author: User
Tags mssql prepare

I used to mainly use the MSSQL, the recent project in the use of MySQL, oneself is a typical small white. Record a MySQL stored procedure today, where you need to split strings and execute SQL statements dynamically.

About string Segmentation I began to use LOCATE and position to do, do not know in proc sometimes error, a separate query can be. Later on the internet to find a MySQL stored procedure string segmentation article, many places are recommended to use SET @result = REVERSE (Substring_index (Substring_index (REVERSE (plate_sku_ Name, ', ', I)), ', ', 1);

As for the execution of SQL , PREPARE SQL1 from @sql1 is required ;   EXECUTE sql1; The feeling is also more complicated than MSSQL.

DELIMITER $$ DROP PROCEDURE IF EXISTS simple_while$$ CREATE PROCEDURE simple_while (in Period_no VARCHAR (7), in all_test_id TEXT, out Return_count INT) BEGIN SET return_count=0; SET @i=0; SET @arraylength=1+ (Length (all_test_id)-Length (REPLACE (all_test_id,',',"'))); While @i<@arraylength do SET @i[Email protected]+1; SET @result= REVERSE (Substring_index (REVERSE (Substring_index (all_test_id,',', @i)),',',1)); SET @sql=concat ('Update Test_', Period_no,'Set is_cancel = 1 WHERE all_test_id =', @result);         PREPARE SQL1 from @sql;         EXECUTE SQL1; SET Return_count=return_count+Row_count ();          END while; end$$ DELIMITER;

MySQL string segmentation and dynamic Execution stitching SQL

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.