MySQL executes the concatenation string statement instance

Source: Internet
Author: User
-- The following is an example of a MySQL concatenation string statement: -- assign a value to the variable to be spliced: SET @ VARNAME

-- The following is an example of a MySQL concatenation string statement: -- assign a value to the variable to be spliced: SET @ VARNAME =

-- The following is an example of MySQL statement for concatenating strings:

-- Assign values to the variables to be spliced

SET @ VARNAME = 'lil ';

-- Concatenate a string, where? Is the parameter for executing the concatenated string statement, and @ TestName is the result value.

SET @ SQLStr0 = CONCAT ('select TestName INTO @ TestName FROM test. t_TestTable WHERE Test_ID>? AND TestName LIKE "% ', @ VARNAME,' %" LIMIT 1 ;');

-- Pre-processing the spliced string

PREPARE SQLStr1 FROM @ SQLStr0;

-- Assign values to parameters

SET @ Test_ID = 1;

-- Run the concatenated string statement using parameters

EXECUTE SQLStr1 USING @ Test_ID;

-- Release the concatenated string statement

Deallocate prepare SQLStr1;

SELECT @ TestName; -- get the result value

-- If you do not need to splice variables or directly use parameters, you can start preprocessing directly.

-- Pre-processing the spliced string

PREPARE SQLStr1 FROM 'select TestName INTO @ TestName FROM test. t_TestTable WHERE Test_ID>? AND TestName LIKE "%? % "LIMIT 1 ;';

-- Assign values to parameters

SET @ Test_ID = 1;

SET @ VARNAME = 'lil ';

-- Run the concatenated string statement using parameters

EXECUTE SQLStr1 USING @ Test_ID, @ VARNAME;

-- Release the concatenated string statement

Deallocate prepare SQLStr1;

SELECT @ TestName; -- get the result value

,

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.