How to write MySQL procedure to insert data into a table?


To write stored procedure to insert data into a table, at first you need to create a table −

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)

Following is the query to write stored procedure to insert data into the table −

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 ;

Following is the query to call the above stored procedure to insert data into the table −

mysql> call StoredProcedureInsertData('Chris',24);
Query OK, 1 row affected (0.18 sec)

Now check the data has been inserted into the table or not −

mysql> select * from insertDataUsingStoredProcedure;

This will produce the following output −

+----+-------+------+
| Id | Name  | Age  |
+----+-------+------+
| 1  | Chris | 24   |
+----+-------+------+
1 row in set (0.00 sec)

Updated on: 30-Jul-2019

461 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements