The Insert Statement in PL/SQL is a fundamental operation in Oracle databases. It allows you to add new records into tables using PL/SQL blocks, procedures, or functions. Understanding how to use the PL/SQL INSERT statement is essential for building applications, automating data loads, or managing transactional systems.
This guide explains the INSERT statement in PL/SQL for beginners and intermediate learners, covering syntax, examples, best practices, and common mistakes.
The INSERT statement in PL/SQL is used to insert one or more rows into an Oracle table. Unlike a standard SQL INSERT, PL/SQL allows the use of procedural logic, variables, and exception handling.
INSERT INTO table_name (column1, column2, column3) VALUES (value1, value2, value3);
BEGIN INSERT INTO employees (emp_id, emp_name, emp_salary) VALUES (101, 'John Smith', 50000); COMMIT; END;
DECLARE v_emp_id NUMBER := 102; v_emp_name VARCHAR2(50) := 'Alice Brown'; v_salary NUMBER := 60000; BEGIN INSERT INTO employees (emp_id, emp_name, emp_salary) VALUES (v_emp_id, v_emp_name, v_salary); COMMIT; END;
This is useful when values are dynamic and not hard-coded.
BEGIN INSERT INTO employee_backup (emp_id, emp_name, emp_salary) SELECT emp_id, emp_name, emp_salary FROM employees WHERE emp_salary > 50000; COMMIT; END;
In PL/SQL, you can insert data from one table into another using the INSERT INTO ... SELECT statement. This method is useful for:
INSERT INTO target_table (column1, column2, column3) SELECT column1, column2, column3 FROM source_table WHERE condition;
Suppose you want to copy all employees earning more than 50,000 from the employees table to employee_backup table:
BEGIN INSERT INTO employee_backup (emp_id, emp_name, emp_salary) SELECT emp_id, emp_name, emp_salary FROM employees WHERE emp_salary > 50000; COMMIT; END;
You can also combine variables with INSERT INTO SELECT for dynamic conditions:
DECLARE v_min_salary NUMBER := 60000; BEGIN INSERT INTO employee_backup (emp_id, emp_name, emp_salary) SELECT emp_id, emp_name, emp_salary FROM employees WHERE emp_salary > v_min_salary; COMMIT; END;
BEGIN INSERT INTO employees (emp_id, emp_name, emp_salary) VALUES (103, 'David Lee', 70000); COMMIT; EXCEPTION WHEN OTHERS THEN ROLLBACK; END;
| Error | Cause |
|---|---|
| ORA-00947 | Mismatch in columns and values |
| ORA-00001 | Duplicate primary key value |
| ORA-01400 | Inserting NULL into NOT NULL column |
| SQL Insert | PL/SQL Insert |
|---|---|
| Runs independently | Runs inside a PL/SQL block |
| No procedural logic | Supports variables and conditions |
| Limited error handling | Advanced exception handling |
The Insert Statement in PL/SQL is essential for managing and adding data in Oracle databases. By using variables, subqueries, loops, and exception handling, developers can perform robust and efficient insert operations. Mastery of the INSERT statement improves application reliability and scalability.
It is used to add new records into tables while supporting variables, procedural logic, and exception handling.
Yes, but changes will not be permanent until a COMMIT is executed. Without it, the insert can be rolled back.
You can use loops or an INSERT INTO SELECT statement to insert multiple rows efficiently.
INSERT adds explicit values, while INSERT INTO SELECT copies data from another table based on conditions.
SQL keywords are not case-sensitive, but string values inserted may be case-sensitive depending on database settings.
Copyrights © 2024 letsupdateskills All rights reserved