- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Insert values in two tables with a single stored procedure call in MySQL
Following is the syntax to insert values in two tables with a stored procedure −
DELIMITER // CREATE PROCEDURE yourProcedureName(anyVariableName int) BEGIN insert into yourTableName1(yourColumnName1) values(yourVariableName); insert into yourTableName2(yourColumnName2) values(yourVariableName); END //
Let us first create a table −
mysql> create table DemoTable1 -> ( -> StudentScore int -> ); Query OK, 0 rows affected (0.58 sec)
Following is the second table −
mysql> create table DemoTable2 -> ( -> PlayerScore int -> ); Query OK, 0 rows affected (0.52 sec)
Here is the query to create a stored procedure and insert values in two tables −
mysql> DELIMITER // mysql> CREATE PROCEDURE insert_proc(value int ) -> BEGIN -> insert into DemoTable1(StudentScore) values(value); -> insert into DemoTable2(PlayerScore) values(value); -> END -> // Query OK, 0 rows affected (0.16 sec) mysql> DELIMITER ;
Now you can call the stored procedure using CALL command −
mysql> call insert_proc(89); Query OK, 1 row affected (0.29 sec)
Display all records from both the tables using select statement −
mysql> select * from DemoTable1333; +--------------+ | StudentScore | +--------------+ | 89 | +--------------+ 1 row in set (0.00 sec) mysql> select * from DemoTable1334; +-------------+ | PlayerScore | +-------------+ | 89 | +-------------+ 1 row in set (0.00 sec)
Advertisements