You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
What is PL/SQL?
What is PL/SQL?
PL/SQL (Procedural Language/SQL) is Oracle's proprietary extension to SQL. It combines the data manipulation power of SQL with the procedural constructs of a programming language, and runs entirely inside the Oracle database engine.
Why PL/SQL Exists
Plain SQL is a declarative language — you describe what data you want, not how to get it. That's great for queries, but insufficient when you need:
- Conditional logic — do different things based on data values
- Loops — process each row in a result set
- Error handling — catch and recover from runtime failures
- Reusable code — encapsulate business logic once, call it many times
PL/SQL fills all of these gaps.
How PL/SQL Fits into Oracle
PL/SQL code executes inside the Oracle database server, not in the application. This brings several benefits:
| Benefit | Detail |
|---|---|
| Performance | Multiple SQL statements execute in one round-trip |
| Security | Applications call named procedures instead of raw SQL |
| Maintainability | Business logic lives in the database, not scattered across apps |
| Portability | Any client can call the same stored procedure |
What You Can Build with PL/SQL
- Anonymous blocks — one-off scripts run interactively
- Stored procedures — reusable named blocks with parameters
- Functions — return a value; callable from SQL
- Triggers — fire automatically on INSERT/UPDATE/DELETE
- Packages — group related procedures and functions together
- Types and collections — complex in-memory data structures
PL/SQL vs Plain SQL
-- Plain SQL: single statement
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
-- PL/SQL: logic + multiple statements in one block
BEGIN
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
/
The PL/SQL version wraps both updates in a transaction with error handling — impossible with a single SQL statement.
Tools for Writing PL/SQL
- SQL*Plus — Oracle's command-line tool, available everywhere
- SQL Developer — free Oracle GUI, excellent for PL/SQL development
- Oracle Live SQL — browser-based Oracle environment, no install needed
- VS Code with Oracle extensions
Key Syntax Note
PL/SQL blocks end with a forward slash (/) on its own line when run in SQL*Plus or SQL Developer scripts. This tells Oracle to execute the block.
Ready to write your first PL/SQL block? Let's go.