INSERT statement in PL/SQL is used to insert new rows in a table. The INSERT statement is part of Data Manipulation Language and allows the user to insert a single record or multiple records into a table.
The syntax for insert statement is as follows:
Syntax:
INSERT INTO table VALUES (value1, value2, value3, ...); INSERT INTO table (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); INSERT INTO table1 (column1, column2) SELECT column1, column2 FROM table2;
If you want to insert multiple rows in a table, you can use the INSERT ALL statement. The syntax for this statement is as follows:
INSERT ALL INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...) INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...) INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...)
You can also insert rows into a table by using a query. The syntax for this is as follows:
INSERT INTO table_name (column1, column2, column3, ...) SELECT column1, column2, column3, ... FROM other_table;
For example, let’s say we want to insert all the rows from the employees table into the new_employees table. We would use the following PL/SQL code:
INSERT INTO new_employees (last_name, first_name, middle_name, gender, hire_date) SELECT last_name, first_name, middle_name, gender, hire_date FROM employees;
Example 1:
INSERT INTO employees VALUES ('Steve', 20, 250); INSERT INTO employees (name, dep_id, salary) VALUES ('David', 10, 300);
Example 2:
INSERT ALL INTO employees (name, dept_id, salary) VALUES ('Anne', 20, 250) INTO employees (name, dept_id, salary) VALUES ('George', 10, 200) INTO employees (name, dept_id, salary) VALUES ('Olivia', 40, 400) SELECT * FROM dual;
Example 3:
INSERT INTO products(id, name, price) SELECT id, name, price FROM old_products;