Home >Database >Mysql Tutorial >How to Pass a String Array to a MySQL Stored Procedure?

How to Pass a String Array to a MySQL Stored Procedure?

DDD
DDDOriginal
2024-12-07 19:47:13852browse

How to Pass a String Array to a MySQL Stored Procedure?

Passing an Array to a MySQL Stored Routine

To pass an array of strings as a parameter to a MySQL stored routine, you can use a prepared statement and build the query string using the CONCAT() function.

DELIMITER $$

CREATE PROCEDURE GetFruits(IN fruitArray VARCHAR(255))
BEGIN

  SET @sql = CONCAT('SELECT * FROM Fruits WHERE Name IN (', fruitArray, ')');
  PREPARE stmt FROM @sql;
  EXECUTE stmt;
  DEALLOCATE PREPARE stmt;

END
$$

DELIMITER ;

How to use:

SET @fruitArray = '\'apple\',\'banana\'';
CALL GetFruits(@fruitArray);

This method will create a temporary table with the fruit names and then use a query to select the corresponding rows from the Fruits table.

The above is the detailed content of How to Pass a String Array to a MySQL 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