![]() |
As the volume and complexity of data continue to grow in modern systems, efficient data management techniques become important. One fundamental operation in database management is the insertion of multiple rows at once. In this article, we understand the techniques and methods available in PL/SQL for inserting multiple rows simultaneously. We will INSERT ALL statements, which allow the insertion of multiple rows in a single query. We will also discuss the INSERT…SELECT statement, which facilitates the insertion of rows by selecting data from another table or query result. Introduction to Insert in PL/SQLThe INSERT statement in PL/SQL is a fundamental tool for adding new records to database tables. It allows developers to insert one or more rows of data into a specified table, either by providing explicit values or by selecting values from another table or query result. The syntax of the INSERT statement is straightforward. Syntax: INSERT INTO table_name (column1, column2, ..., column_n) Explanation:
Setting Up EnvironmentLet us start by creating a sample table. We will create an employee table with fields like employee_id, employee_name, and city. The following query creates the table: Query: CREATE TABLE employees We are going to have a look at two methods in this article to go about inserting multiple records in the table. Ways to Insert Multiple Rows at Once in PL/SQLMethod 1: Using INSERT ALLThe INSERT ALL statement is used to insert multiple records into the table using a single query. Syntax: INSERT ALL Explanation:
Example: Let’s inserts 3 records in the employees table The following query inserts 3 records in the employees table. Query: INSERT ALL INTO employees The following query prints the contents of the table after the insert operation: Query: DECLARE Output: ![]() Output Explanation: In the above PL/SQL query we utilizes the INSERT ALL statement to efficiently insert multiple rows into the ‘employees‘ table with predefined values. Subsequently, a cursor is created to fetch the inserted data, and each record’s details, including employee ID, name, and city, are displayed using the DBMS_OUTPUT.PUT_LINE procedure. Method 2: Using INSERT…SELECTThe INSERT…SELECT statement is used to insert multiple records into the table using a single query. Syntax: INSERT INTO dest_table_name(column1, column2, column_n) Explanation:
Example The following query inserts 3 records in the employees table. Query: INSERT INTO employees The following query prints the content of the table after insertion: Query: DECLARE Output: ![]() Output Explanation: In the above PL/SQL query we utilizes the INSERT INTO statement in combination with SELECT and UNION ALL clauses to insert multiple rows of data into the ‘employees‘ table. Subsequently, a cursor is created to fetch the inserted data, and each record’s details, including employee ID, name, and city, are displayed using the DBMS_OUTPUT.PUT_LINE procedure. ConclusionOverall, After reading whole article now you have good understanding about how to insert multiple row at once through various methods like Using INSERT ALL and Using INSERT…SELECT method. We have implemented these method and saw the example along with the output and their explanations. Now you can easily use these method and insert records into the tables easily. |
Reffered: https://www.geeksforgeeks.org
Databases |
Related |
---|
![]() |
![]() |
|
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |