MySQL stored Procedure pass parameter implementation where ID in (1,2,3,...) Sample _mysql

Source: Internet
Author: User
Normal wording:
Copy Code code as follows:

SELECT * FROM table_name t where T.field1 in (1,2,3,4,...);

When the list in the write stored procedure is entered with an incoming parameter, the following methods are used:

Mainly used to Find_in_set function
Copy Code code as follows:

SELECT * FROM table_name t where Find_in_set (t.field1, ' 1,2,3,4 ');

Of course, the more stupid way is to assemble the string and then execute:
Copy Code code as follows:

DROP PROCEDURE IF EXISTS Photography. Proc_test;
CREATE PROCEDURE Photography. ' Proc_test ' (param1 varchar (1000))
BEGIN
Set @id = param1;
Set @sel = ' SELECT * Access_record t where t.id in (';
Set @sel_2 = ') ';
Set @sentence = Concat (@sel, @id, @sel_2);--The connection string generates the SQL statement to execute
prepare stmt from @sentence;--to be prepared. The name of the "stmt" precompiled variable,
execute stmt;--Execute SQL statement
Deallocate prepare stmt;--release resource
end;

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.