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
Release: 2024-12-07 19:47:13
Original
856 people have browsed it

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 ;
Copy after login

How to use:

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

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template