MySQL 存储过程传参的一个坑
例如一个存储过程:
delimiter //
create procedure read_score(IN id int)
begin
select * from score where id=id;
end;
//
delimiter ;
在这种情况下,无论我传入id是什么值,这个存储过程都会返回score的所有数据。
原因是当传入参数的名字跟字段名字相等的时候,那么参数名字就不会替换成实际参数,上述例子的SQL也就是始终是执行SQLselect * from score where id=id;
,因为id=id恒为真,所以实际等效于select * from score
对于查询还好,万一SQL是delete from score where id=id
呢?所以需要特别注意,解决方法就是形式参数要跟表字段区分开来,例如
create procedure read_score(IN prd_id int)
begin
select * from score where id=prd_id;
end;
//
还没有评论,来说两句吧...