← PL/SQL Topics

Packages & Modularity

Explain PL/SQL package initialization blocks

Package Initialization Blocks

Oracle Fusion / EBS · Technical · PL/SQL

GeneralHigh confidence

An unnamed block at the end of a package body that runs once per session, before the first real call.

Grounded in: Curated Oracle knowledge layer — PL/SQL

How it works

A package body can end with its own unnamed BEGIN...END block, placed after all the procedure and function bodies — this runs exactly once per session, the first time anything in the package is referenced, before that first call actually executes. It's the standard place to prime package-level variables (load a lookup value, start a session-scoped timer) without requiring every caller to remember to invoke a separate 'init' procedure.

How it works
CREATE OR REPLACE PACKAGE BODY session_ctx AS
  PROCEDURE set_user(p_user_id NUMBER) IS
  BEGIN
    g_current_user_id := p_user_id;
  END;
BEGIN
  -- Initialization block: runs once, the first time this package
  -- is referenced in the session.
  g_current_user_id := -1; -- 'not yet set' sentinel
END session_ctx;
/

Related Questions

Have a follow-up, or a different question?

Continue in Ask Oracle AI