How to write a procedure to insert data in the table in phpMyAdmin?

MySQLMySQLi Database

Let us first create a new table and understand the concept in continuation

mysql> create table StoredProcedureInsertDemo
   -> (
   -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> UserName varchar(20),
   -> UserAge int
   -> );
Query OK, 0 rows affected (0.63 sec)

Here is the query to create a stored procedure to insert data in to the table

mysql> DELIMITER //
mysql> create procedure procedure_InsertIntoTable(IN FirstName VARCHAR(100),IN Age INT)
   -> BEGIN
   -> insert into StoredProcedureInsertDemo(UserName,UserAge) values (FirstName,Age);
   -> END
   -> //
Query OK, 0 rows affected (0.34 sec)
mysql> DELIMITER ;

Call the stored procedure with the help of CALL command as shown below

mysql> call procedure_InsertIntoTable('Larry',23);
Query OK, 1 row affected, 1 warning (0.19 sec)

Check the table records once again.

The query is as follows −

mysql> select *from StoredProcedureInsertDemo;

The following is the output

+--------+----------+---------+
| UserId | UserName | UserAge |
+--------+----------+---------+
|      1 | Larry    | 23      |
+--------+----------+---------+
1 row in set (0.00 sec)
raja
Published on 01-Apr-2019 13:27:24
Advertisements