Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use WHERE IN mysql stored procedure

How do I pass an array and use WHERE IN inside stored procedure?

Do i need to concatenate input string or something ?

Lets say

DELIMITER $$
DROP PROCEDURE IF EXISTS `abc`.`table1`$$
CREATE PROCEDURE  `abc`.`test`
(IN somestring VARCHAR(255))
BEGIN
    SELECT * FROM abc.table1 
    WHERE flight_type IN somestring
END $$
DELIMITER ;
like image 322
flyclassic Avatar asked Mar 21 '11 03:03

flyclassic


People also ask

Can I use stored procedure in where clause?

You can use temporary table instead stored procedure into where clause. Hi Rox, It would be better to convert your Stored Procedure into a Function, if possible. Then you can use function in WHERE clause.

Where are stored procedures in MySQL?

Where are stored procedures stored? Stored procedures are stored in the mysql. routines and mysql. parameters tables, which are part of the data dictionary.

How do you find where a stored procedure is used?

Using SQL Server Management Studio Expand Databases, expand the database in which the procedure belongs, and then expand Programmability. Expand Stored Procedures, right-click the procedure and then click View Dependencies. View the list of objects that depend on the procedure.

How do I execute a stored procedure in MySQL?

Create a simple stored procedure. DELIMITER ; To create the MySQL Stored Procedure, open the MySQL workbench Connect to the MySQL Database copy-paste the code in the query editor window click on Execute. You can view the procedure under stored procedures.


1 Answers

You can use the string concatenation and the PREPARE statement to run dynamically built queries.

somestring must be constructed in a valid SQL format like '1','2','3'

DELIMITER $$
DROP PROCEDURE IF EXISTS `abc`.`table1`$$
CREATE PROCEDURE  `abc`.`test`
(IN somestring VARCHAR(255))
BEGIN
    @s=CONCAT("
    SELECT * FROM abc.table1 
    WHERE flight_type IN (",somestring,");")
    PREPARE stmt FROM @s;
    EXECUTE @s;
END $$
DELIMITER ;
like image 57
Pentium10 Avatar answered Sep 21 '22 16:09

Pentium10