SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
PL/SQL (Procedural Language for SQL) is Oracle Corporation's procedural extension for SQL and the Oracle relational database. Essentially, PL/SQL augments the standard SQL language with procedural capabilities, similar to those found in procedural languages like C or Pascal. This allows developers to write code that can process and manipulate data, control program flow, handle exceptions, and generally execute procedural logic in the database itself.
Block Structure: PL/SQL uses a block structure for organizing code. A block can be thought of as a unit of code. Blocks can be nested inside other blocks.
Procedural Constructs: Provides constructs such as loops, conditions, and exception handling, allowing for a robust programming environment.
Variables and Types: PL/SQL supports various data types, including scalar types (like numbers, dates, and strings), LOBs, collections, and user-defined types.
Cursors: Used to process multiple rows retrieved by SELECT statements.
Stored Procedures and Functions: PL/SQL allows you to create stored procedures (which don't return values) and functions (which do return values) that can be saved in the database and reused.
Triggers: PL/SQL enables the creation of triggers, which are special types of stored procedures that run automatically when certain events occur in the database.
Exception Handling: Allows developers to define, detect, and handle runtime errors gracefully.
Packages: A higher-level organizational feature that groups related PL/SQL types, variables, constants, subprograms, cursors, and exceptions.
Performance: Since PL/SQL allows for entire blocks of statements to be sent to Oracle at once, it can reduce network traffic and boost performance.
Productivity: Reusable and organized code (via procedures, functions, and packages) can increase developer productivity.
Security: With PL/SQL, you can encapsulate and centralize business logic, making it easier to manage security policies on that logic.
Integrated with Oracle: PL/SQL is deeply integrated with the Oracle Database, offering direct access to SQL features and tight cohesion with Oracle tools and features.
Error Handling: Provides advanced error handling capabilities for managing exceptions and ensuring that applications can manage and report errors gracefully.
DECLARE message VARCHAR2(20) := 'Hello, PL/SQL!'; BEGIN DBMS_OUTPUT.PUT_LINE(message); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred.'); END;
In the example above:
DECLARE
section is used to declare any variables or initializations.BEGIN
section contains the procedural code.DBMS_OUTPUT.PUT_LINE
is a procedure used to display output.EXCEPTION
section is used to handle any runtime errors.While PL/SQL is mainly associated with the Oracle Database, similar procedural extensions are available for other RDBMS systems, like T-SQL for Microsoft SQL Server and PL/pgSQL for PostgreSQL.