且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在MySQL存储过程中按ID获取行?

更新时间:2023-01-30 23:30:07

该行为是由于将输入参数命名为与列名相同而引起的,因此mysql无法区分列和where子句中的参数.将输入参数重命名为param_ID,它将返回带有请求的ID值的记录.

The behaviour is caused by naming the input parameter same as the column name, therefore mysql cannot distinguish between the column and the parameter within the where clause. Rename the input parameter to let's say param_ID and it will return the record with the requested ID value.

DELIMITER $$
CREATE PROCEDURE `buscarPaciente`(IN param_ID INT)
    BEGIN
        SELECT * FROM Paciente WHERE ID = param_ID LIMIT 1;
    END
$$
DELIMITER ;