如何在条件下将varchar字符串传递给存储过程?
在这里,在没有工作的情况下如何解决这个问题?

CALL searchStudentByName("%AAP%","'1','2','3'");


MYSQL存储过程:

DELIMITER $$

USE `studentsdb`$$

DROP PROCEDURE IF EXISTS `searchStudentByName`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `searchStudentByName`(IN `search_key` VARCHAR(40),IN `student_ids` VARCHAR(350))
BEGIN
    SELECT students.StudentId,students.StudentName FROM students WHERE students.Status='A' AND students.StudentName LIKE search_key AND students.StudentId NOT IN(student_ids) LIMIT 5;
END$$

DELIMITER ;

最佳答案

您确实需要准备一条语句来使用类似的值列表。尝试这个:

CREATE DEFINER=`root`@`localhost` PROCEDURE `searchStudentByName`(IN `search_key` VARCHAR(40),IN `student_ids` VARCHAR(350))
BEGIN
    SET @sql = CONCAT("SELECT students.StudentId,students.StudentName FROM students WHERE students.Status='A' AND students.StudentName LIKE '", search_key, "' AND students.StudentId NOT IN(", student_ids, ") LIMIT 5");
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END$$

关于mysql - 如何在条件下将varchar字符串传递给存储过程?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53442082/

10-12 14:23