我们可以使用IN运算符创建一个存储过程来更新MySQL表中的值。为了理解它,我们以一个名为“ student_info”的表为例,该表包含以下数据:
mysql> Select * from student_info; +------+---------+------------+------------+ | id | Name | Address | Subject | +------+---------+------------+------------+ | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Jaipur | Literature | | 110 | Rahul | Chandigarh | History | | 125 | Raman | Bangalore | Computers | +------+---------+------------+------------+ 4 rows in set (0.01 sec)
现在,通过如下创建名为“ update_studentinfo”的过程,我们可以更新“ student_info”表中的值-
mysql> DELIMITER // ; mysql> Create Procedure Update_studentinfo ( IN p_id INT, IN p_address varchar(20)) -> BEGIN -> UPDATE student_info -> SET -> address = p_address WHERE id = p_id; -> END //
现在,使用我们要在表中更新的值调用该过程,如下所示:
mysql> Delimiter ; // mysql> CALL Update_studentinfo(125, 'Shimla'); mysql> Select * from student_info; +------+---------+------------+------------+ | id | Name | Address | Subject | +------+---------+------------+------------+ | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Jaipur | Literature | | 110 | Rahul | Chandigarh | History | | 125 | Raman | Shimla | Computers | +------+---------+------------+------------+ 4 rows in set (0.00 sec)
上面的结果集表明,表中更新了ID = 125的记录的地址。