如何编写 MySQL 过程将数据插入表中?
要编写存储过程以将数据插入表中,首先需要创建一个表 −
mysql> create table insertDataUsingStoredProcedure -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY , -> Name varchar(20), -> Age int -> ); Query OK, 0 rows affected (0.51 sec)
以下是写入存储过程以将数据插入表的查询 −
mysql> DELIMITER // mysql> CREATE PROCEDURE StoredProcedureInsertData(IN StudentName varchar(100),IN StudentAge int) -> BEGIN -> insert into insertDataUsingStoredProcedure(Name,Age) values (StudentName, StudentAge ); -> END -> // Query OK, 0 rows affected (0.13 sec) mysql> DELIMITER ;
以下是调用上述存储过程以将数据插入表的查询 −
mysql> call StoredProcedureInsertData('Chris',24); Query OK, 1 row affected (0.18 sec)
现在检查数据是否已插入表中 −
mysql> select * from insertDataUsingStoredProcedure;
这将产生以下输出 −
+----+-------+------+ | Id | Name | Age | +----+-------+------+ | 1 | Chris | 24 | +----+-------+------+ 1 row in set (0.00 sec)
广告