DB2advanced

DB2 Stored Procedures

Understand why DB2 stored procedures exist, how they're written and called from COBOL, and what tradeoffs they introduce versus embedded SQL.

A DB2 stored procedure packages business logic (often written in COBOL itself, though other languages are supported) so it runs directly within the database engine's address space, callable from any client via a simple CALL statement instead of requiring that client to embed and maintain the underlying SQL and logic itself. This matters for interviews because it represents a genuine architectural choice — centralizing logic in the database tier versus keeping it in application programs — with real tradeoffs interviewers expect you to articulate.

A stored procedure is a shared specialist office embedded inside the courthouse itself (the database) that any department can call on for a specific well-defined service, rather than every department keeping its own in-house copy of the same specialist's knowledge and risking each copy drifting out of sync over time.

Key Concepts

1
A stored procedure is written like a normal program (frequently COBOL, callable via standard EXEC SQL CALL syntax from any client language), registered with DB2 via CREATE PROCEDURE specifying its parameters, language, and the load module to invoke, and then runs in a DB2-managed address space (a stored procedure address space, or WLM-managed environment) separate from the calling application's own address space. Parameters are declared IN, OUT, or INOUT, giving the procedure a clear contract for what data flows in and what results flow back to the caller.
2
The key benefit is centralizing complex or frequently repeated data-access logic in one place, callable identically from CICS, batch, or even non-mainframe clients over a network connection, rather than duplicating the same embedded SQL and business rules across many separate calling programs. The tradeoff is added architectural complexity — debugging spans two runtime contexts (caller and procedure), and stored procedure address space configuration/tuning becomes its own operational concern.
3
A balanced interview answer avoids treating stored procedures as an unconditional best practice — they're the right tool when centralizing shared, data-intensive logic genuinely reduces duplication and network round-trips (especially for remote/distributed callers), but overusing them for simple single-table logic that could just as easily live as embedded SQL in the calling program adds operational overhead without a clear corresponding benefit.