Using SQL*Plus in Oracle โ€“ Modes, Commands, Tips & Examples

What is PL/SQL Statement Processing?

When a PL/SQL block is executed in Oracle, it goes through a structured multi-phase processing flow. The control is transferred between the PL/SQL Engine and SQL Engine, depending on the type of statements involved. This internal mechanism ensures efficient handling of both procedural and SQL parts of a PL/SQL program.

Understanding this internal execution flow is crucial for:

  • Writing optimized code

  • Debugging execution lags

  • Preparing for Oracle interviews

  • Enhancing application performance

Why It Matters?

Unlike typical procedural languages, PL/SQL integrates SQL and procedural logic, and these components are handled differently at runtime. Knowing which part is processed where helps in:

  • Reducing context switches

  • Minimizing performance bottlenecks

  • Understanding execution plans

Statement Processing Flow in PL/SQL

When a PL/SQL block is submitted for execution, the following steps occur:

1. Parsing

The PL/SQL engine first parses the code to:

  • Validate syntax

  • Resolve variable names

  • Prepare SQL statements for execution

2. Engine Dispatch

The PL/SQL engine separates the code into:

  • SQL statements โ†’ Handled by the SQL Engine

  • Procedural statements โ†’ Handled by the Procedural Statement Executor

This separation is essential because the SQL engine cannot process IF-ELSE, LOOPS, or variable declarations, and the PL/SQL engine doesnโ€™t execute SQL statements like SELECT, INSERT, or UPDATE.

3. Context Switching

Every time control switches between SQL and procedural logic, a context switch occurs. This can slightly affect performance and is a critical tuning point.

4. Execution

  • Procedural logic is executed by the PL/SQL engine.

  • SQL commands are executed by the SQL engine.

  • The results are combined and handled by the Oracle Runtime Engine.

5. Output

The final result or output is shown using tools like DBMS_OUTPUT, or committed into tables based on the DML actions.

Example: PL/SQL Block with SQL and Procedural Statements

SET SERVEROUTPUT ON;

DECLARE
   v_total NUMBER;
BEGIN
   -- SQL statement
   SELECT COUNT(*) INTO v_total FROM employees;

   -- Procedural logic
   IF v_total > 0 THEN
      DBMS_OUTPUT.PUT_LINE('Total Employees: ' || v_total);
   ELSE
      DBMS_OUTPUT.PUT_LINE('No employees found.');
   END IF;
END;
/

Breakdown:

  • SELECT โ†’ SQL engine
  • IF-ELSE, DBMS_OUTPUT โ†’ PL/SQL engine

SQL Engine vs PL/SQL Engine

FeatureSQL EnginePL/SQL Engine
Primary RoleExecutes SQL statementsExecutes procedural logic
HandlesSELECT, INSERT, UPDATE, DELETE, etc.Loops, IF-ELSE, EXCEPTION, DECLARE
OptimizationUses cost-based optimizerNo query optimization
Context SwitchOccurs when PL/SQL uses SQL inside itSends SQL to SQL Engine when needed
Data AccessDirectly accesses and manipulates dataUses SQL to access data
SpeedFast for large, set-based operationsSlower for row-by-row or complex logic
Can Execute Procedural Logic?NoYes
Can Execute SQL?YesOnly by calling SQL Engine
Used InQueries, DML, DDLProcedures, Functions, Triggers

Performance Tips

  1. Reduce context switches:

    • Use BULK COLLECT and FORALL for better performance when working with collections.

  2. Avoid unnecessary SQL in loops:

    • Move SQL queries outside loops where possible.

  3. Use Native Compilation:

    • Use PL/SQL native compilation (NCOMP) for faster execution.

When to Optimize Statement Flow

  • When you notice latency in PL/SQL loops

  • If you perform multiple SQL calls inside procedural loops

  • While tuning high-throughput batch programs

FAQ Section

PL/SQL Statement Processing refers to how the Oracle engine handles SQL and procedural code separately during block execution.

Itโ€™s the overhead of switching between SQL and procedural engines during execution, often a performance concern.

Yes. Use FORALL, BULK COLLECT, and avoid executing SQL inside loops.

๐Ÿ“˜ AmantPoint Exclusive Learning Series
ยฉ 2025 AmantPoint. All rights reserved. Educational content for developers and learners.

Scroll to Top