如何在 MySQL 表中创建存储过程来插入值?
我们可以使用带 IN 运算符的存储过程在 MySQL 表中插入值。为了理解这一点,我们以一个名为“student_info”的表为例,其中有以下数据——
mysql> Select * from student_info; +------+---------+-----------+------------+ | id | Name | Address | Subject | +------+---------+-----------+------------+ | 100 | Aarav | Delhi | Computers | | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Jaipur | Literature | | 110 | Rahul | Chandigarh | History | +------+---------+------------+------------+ 4 rows in set (0.00 sec)
现在,通过按照如下方式创建名为“insert_studentinfo”的存储过程,我们可以将值插入“student_info”表——
mysql> DELIMITER // ; mysql> Create PROCEDURE insert_studentinfo(IN p_id int, IN p_name varchar(20),IN p_Address Varchar(20), IN p_subject Varchar(20)) -> BEGIN -> insert into student_info(id, name, address, subject) values (p_id, p_name,p_address, p_subject); -> END // Query OK, 0 rows affected (0.09 sec)
现在,按照如下方式,用我们想要插入到表中的值调用该过程——
mysql> CALL insert_studentinfo(125,'Raman','Bangalore','Computers')// Query OK, 1 row affected (0.78 sec) mysql> DELIMITER ; // mysql> Select * from Student_info; +-----+---------+------------+------------+ | id | Name | Address | Subject | +-----+---------+------------+------------+ | 100 | Aarav | Delhi | Computers | | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Jaipur | Literature | | 110 | Rahul | Chandigarh | History | | 125 | Raman | Bangalore | Computers | +------+---------+------------+-----------+ 5 rows in set (0.00 sec)
以上结果集显示这些值已插入表中。
广告