Home  >  Article  >  Database  >  How to Pass a String of Values to an IN Clause in a Stored Procedure?

How to Pass a String of Values to an IN Clause in a Stored Procedure?

Susan Sarandon
Susan SarandonOriginal
2024-10-26 04:28:30916browse

How to Pass a String of Values to an IN Clause in a Stored Procedure?

Passing Variables to an IN Clause

Consider a stored procedure with a SELECT statement that includes an IN clause:

SELECT product_id, product_price FROM product
WHERE product_type IN ('AA','BB','CC');

If you wish to populate the IN clause values from a single variable containing a string of values, for instance:

'AA,BB,CC'

You may encounter difficulties using this approach. Here's a solution:

Instead of attempting to pass the string directly to the IN clause, pass it as a parameter value in the following format:

'AA,BB,CC'

Utilize the FIND_IN_SET function to determine whether each product_type value is present in the input parameter:

SELECT product_id, product_price
FROM product
WHERE FIND_IN_SET(product_type, param);

In this modified statement, param represents the input parameter containing the string of values.

The above is the detailed content of How to Pass a String of Values to an IN Clause in a Stored Procedure?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn