
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Can we use stored procedure to insert records into two tables at once in MySQL?
Yes, you can use stored procedure to insert into two tables in a single query. Let us first create a table −
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentFirstName varchar(20) ); Query OK, 0 rows affected (0.56 sec)
Here is the query to create second table −
mysql> create table DemoTable2 ( ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, ClientName varchar(20), ClientAge int ); Query OK, 0 rows affected (0.76 sec)
Following is the query to create stored procedure to insert into two tables created above −
mysql> DELIMITER // mysql> CREATE PROCEDURE insert_into_twoTables(name varchar(100),age int) BEGIN INSERT INTO DemoTable(StudentFirstName) VALUES(name); INSERT INTO DemoTable2(ClientName,ClientAge) VALUES(name,age); END // Query OK, 0 rows affected (0.14 sec) mysql> DELIMITER ;
Now call the stored procedure with the help of CALL command −
mysql> call insert_into_twoTables('Tom',38); Query OK, 1 row affected, 1 warning (0.41 sec)
Check the record is inserted into both tables or not.
The query to display all records from the first table is as follows −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+------------------+ | StudentId | StudentFirstName | +-----------+------------------+ | 1 | Tom | +-----------+------------------+ 1 row in set (0.00 sec)
Following is the query to display all records from the second table −
mysql> select *from DemoTable2;
This will produce the following output −
+----------+------------+-----------+ | ClientId | ClientName | ClientAge | +----------+------------+-----------+ | 1 | Tom | 38 | +----------+------------+-----------+ 1 row in set (0.00 sec)
- Related Articles
- Insert values in two tables with a single stored procedure call in MySQL
- Use UNION ALL to insert records in two tables with a single query in MYSQL
- How can we invoke MySQL stored procedure?
- How can we alter a MySQL stored procedure?
- How can we drop a MySQL stored procedure?
- How to correctly use delimiter in a MySQL stored procedure and insert values?
- Can I insert two or more rows in a MySQL table at once?
- Insert records from multiple tables in MySQL
- How can we use prepared statements in a stored procedure?
- How can we write MySQL handler in a stored procedure?
- How can we access tables through MySQL stored procedures?
- How can we use WHERE clause with MySQL INSERT INTO command?
- MySQL Stored Procedure to update records with certain condition?
- What is stored procedure and how can we create MySQL stored procedures?
- How can I create a stored procedure to insert values in a MySQL table?

Advertisements