Question

I want to create a procedure for a select query in which the where clause will have a IN Clause. I had created one procedure like below butu its not working-

CREATE DEFINER = `root`@`localhost` PROCEDURE `agentin` ( IN `code` VARCHAR( 100 ) ) NOT DETERMINISTIC NO SQL SQL SECURITY DEFINER 
BEGIN 
SELECT * FROM AgentInformation WHERE AgentCode IN (code);
END

After putting the In clause values like --> IN ('CG001','CG002')

I am getting null values and the query made by phpmyadmin was

SET @p0 = '''CG001'',''CG002''';

CALL agentin ( @p0 );

Please help regarding it , Thanks

Was it helpful?

Solution

Here it is an example -

DELIMITER $$

CREATE PROCEDURE procedure1(IN id_param VARCHAR(255))
BEGIN
  SET @sql = CONCAT('SELECT * FROM table WHERE id IN (', id_param, ')');
  PREPARE stmt FROM @sql;
  EXECUTE stmt;
  DEALLOCATE PREPARE stmt;
END$$

DELIMITER ;

SET @id = '1,2,3,4,5';
CALL procedure1(@id);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top