Create a table inside a MySQL stored procedure and insert a record on calling the procedure


Create a table inside the stored procedure and use INSERT as well −

mysql> DELIMITER //
mysql> CREATE PROCEDURE create_TableDemo(id int,name varchar(100),age int)
   BEGIN
   CREATE TABLE DemoTable
   (
      ClientId int NOT NULL,
      ClientName varchar(30),
      ClientAge int,
      PRIMARY KEY(ClientId)
   );
   INSERT INTO DemoTable VALUES(id,name,age);
   SELECT *FROM DemoTable;
   END
//
Query OK, 0 rows affected (0.17 sec)
mysql> DELIMITER ;

Call the stored procedure using CALL command −

mysql> CALL create_TableDemo(100,'Robert',28);

This will produce the following output −

+----------+------------+-----------+
| ClientId | ClientName | ClientAge |
+----------+------------+-----------+
|      100 | Robert     |        28 |
+----------+------------+-----------+
1 row in set (0.76 sec)
Query OK, 0 rows affected (0.78 sec)

Updated on: 04-Oct-2019

421 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements