PL/SQL
Oracle's procedural extension to SQL, a block-structured, Ada-influenced language for writing stored procedures, functions, packages, and triggers that run inside the Oracle Database.
Created by Oracle Corporation
PL/SQL (Procedural Language/SQL) is Oracle’s procedural extension to the SQL query language. It wraps ordinary SQL in the control structures of a conventional programming language — variables, assignments, loops, conditionals, and exception handling — so that complex logic can be packaged and executed inside the Oracle Database itself as stored procedures, functions, packages, and triggers. Where plain SQL describes what data to retrieve or change, PL/SQL lets developers describe how a sequence of database operations should be carried out, all within a single, server-side, transaction-aware program unit.
History and Origins
PL/SQL grew out of work that began at Oracle Corporation around 1987, when the first lines of the PL/SQL compiler were written. The language first reached users in 1991, when PL/SQL 1.0 was bundled with the Oracle Database as part of the Transaction Processing Option for Oracle 6. This earliest version was deliberately modest: it offered flow-of-control statements and scalar data types but could not yet define stored procedures, manipulate arrays, or perform operating-system input and output.
The designers modeled PL/SQL’s syntax on Ada — the strongly typed, block-structured language developed for the U.S. Department of Defense — which in turn drew its lineage from Pascal. That heritage is visible throughout PL/SQL: its DECLARE/BEGIN/EXCEPTION/END blocks, its strong static typing, its package construct (echoing Ada packages), and its %TYPE/%ROWTYPE anchored declarations all reflect Ada-style design.
The language matured rapidly with the Oracle 7 family. PL/SQL 2.0, shipped with Oracle 7 in 1992, was a turning point: it added stored procedures, functions, packages, and database triggers, transforming PL/SQL from a scripting convenience into a full server-side programming environment. The 2.1, 2.2, and 2.3 point releases tracked Oracle 7.1 through 7.3, refining collections, records, and dynamic SQL.
Design Philosophy
PL/SQL is built around a few guiding ideas:
- Reduce round trips. By moving procedural logic into the database server, a block of computation that would otherwise require many client-to-server SQL statements can execute as a single call, with intermediate results staying inside the database. This matters most over high-latency connections and for high-volume transaction processing.
- First-class access to SQL. SQL statements are written directly in the language rather than through a separate API. Query results flow into variables and records, and values from variables substitute seamlessly into SQL.
- Block structure. Every PL/SQL program is organized into blocks of the form
DECLARE ... BEGIN ... EXCEPTION ... END. Blocks nest, each introducing its own scope of declared variables, and the optionalEXCEPTIONsection provides structured error recovery. - Tight type integration. Variables are declared with Oracle’s own SQL data types. Anchored declarations such as
%TYPEand%ROWTYPElet a variable automatically adopt the type of a table column or an entire row, so code stays correct when the schema changes. - Packaging and encapsulation. Related procedures, functions, types, and variables are grouped into packages with a public specification and a private body — a unit of modularity that also improves performance by loading and caching code together.
Key Features
A typical PL/SQL routine combines declarations, ordinary SQL, and control flow:
| |
The language’s notable capabilities include:
- Control structures —
IF/THEN/ELSIF/ELSE,CASE, and several loop forms (LOOP,WHILE, numeric and cursorFOR), plusEXITandCONTINUE. - Stored program units — procedures, functions, packages, and triggers, all stored in compiled form inside the database and callable from SQL or other PL/SQL.
- Records and collections —
RECORDtypes group related fields; collection types (associative arrays, nested tables, and VARRAYs) provide array-like structures. - Cursors — explicit cursors and
FOR ... IN (SELECT ...)loops iterate over query result sets row by row, with cursor attributes such as%FOUNDand%ROWCOUNT. - Exception handling — the
EXCEPTIONblock traps predefined errors (such asNO_DATA_FOUNDorZERO_DIVIDE) and user-defined exceptions, enabling clean error recovery. - Bulk operations —
BULK COLLECTandFORALL(introduced in Oracle8i) batch many rows between the PL/SQL and SQL engines in one context switch, a major performance optimization for set-oriented work. - Dynamic SQL — native dynamic SQL via
EXECUTE IMMEDIATE, plus the olderDBMS_SQLpackage, build and run statements assembled at runtime. - Triggers — trigger bodies fire on data or DDL events with access to affected rows through the
:NEWand:OLDpseudo-records.
Evolution
PL/SQL has matured alongside the Oracle Database rather than through standalone releases. After Oracle8 synchronized the language’s version numbers with the database in 1997 (and added object types and methods), each subsequent database release extended PL/SQL:
- Oracle8i (1999) added bulk SQL (
BULK COLLECT,FORALL) and native dynamic SQL (EXECUTE IMMEDIATE). - Oracle9i (2001) introduced PL/SQL native compilation — translating procedural code into native machine code through a C compiler to accelerate computation-heavy logic — and brought object-type inheritance in Release 2.
- Oracle Database 11g added real native compilation that no longer required an external C toolchain and function result caching (Release 1, 2007), plus edition-based redefinition for online upgrades (Release 2, 2009). (The PL/SQL optimizing compiler had been introduced earlier, in Oracle Database 10g.)
- Oracle Database 12c (2013) allowed PL/SQL functions and procedures to be declared directly in a SQL statement’s
WITHclause and raised several size limits. - Oracle Database 23ai (2024) added the
SQL_TRANSPILER, which automatically converts eligible PL/SQL functions into equivalent SQL expressions for better optimization, among other in-database programming enhancements.
Because the language ships with the server, its effective “version” tracks the Oracle Database release. The latest long-term release, Oracle Database 23ai, became generally available on May 2, 2024.
Current Relevance
PL/SQL remains one of the most widely deployed procedural database languages in the world, a direct consequence of Oracle’s dominant position in enterprise relational databases. Banking, telecommunications, government, and large-scale ERP systems run substantial business logic in PL/SQL packages and triggers. Oracle’s own products lean on it heavily: E-Business Suite implements much of its logic in PL/SQL, and Oracle APEX, Oracle’s low-code application platform, is built around PL/SQL for server-side processing.
The language’s influence also reaches beyond Oracle. PostgreSQL’s PL/pgSQL was deliberately modeled on PL/SQL to ease adoption and migration, and IBM Db2 (from version 9.7) and Oracle’s TimesTen in-memory database both added PL/SQL compatibility so that existing Oracle procedures could run with minimal change. PL/SQL also relates to the ISO SQL/PSM standard for persistent stored modules, which standardizes the broader idea of a procedural language embedded in SQL.
Why It Matters
PL/SQL helped define what it means to put logic close to the data. By embedding a strongly typed, Ada-influenced procedural language directly inside the database — and giving SQL first-class status within it — Oracle gave enterprises a way to enforce correctness, performance, and atomicity in the same transaction that touched the data. That model proved durable enough to be imitated by competitors and standardized by ISO, and it remains central to how some of the largest production systems in the world are built. More than three decades after PL/SQL 1.0, the language is still the default tool for server-side programming in the Oracle ecosystem.
Sources: PL/SQL — Wikipedia; PL/SQL — Oracle FAQ; A (Not So) Brief But (Very) Accurate History of PL/SQL — Oracle Internals; The Origins of PL/SQL — Oracle PL/SQL Programming, O’Reilly; Oracle Database 23ai New Features Guide.
Timeline
Notable Uses & Legacy
Oracle E-Business Suite and enterprise applications
Large Oracle application suites such as E-Business Suite implement extensive business logic in PL/SQL packages and triggers running inside the database, keeping complex transactional rules close to the data.
Oracle APEX (Application Express)
Oracle's low-code web development platform is built on PL/SQL: page processing, validations, and server-side logic are written as PL/SQL stored in the database, with the framework itself authored largely in the language.
Stored procedures, packages, and triggers
Across banking, telecom, and government systems, PL/SQL packages encapsulate validation, auditing, and batch data transformations as server-side units, reducing client-server round trips and enforcing rules centrally.
Database triggers and integrity logic
PL/SQL trigger bodies react to INSERT, UPDATE, and DELETE events to maintain audit trails, derived columns, and cross-table constraints, with access to the affected rows through the :NEW and :OLD pseudo-records.
Db2 and TimesTen PL/SQL compatibility
IBM Db2 (from version 9.7) and Oracle's TimesTen in-memory database support PL/SQL, allowing procedures written for Oracle to run with minimal change and easing migration between platforms.
Language Influence
Influenced By
Influenced
Running Today
Run examples using the official Docker image:
docker pull gvenzl/oracle-freeExample usage:
docker run --rm -e ORACLE_PASSWORD=secret gvenzl/oracle-free