OCILIB Tutorials v5.0.0
Beginnerdemo/output.c

DBMS_OUTPUT — server-side print capture

Objectives — by the end of this page you will be able to:

Concepts

DBMS_OUTPUT.PUT_LINE writes to a session buffer that clients can read after executing PL/SQL — Oracle's printf debugging. The connection exposes it:

C function C++ method Purpose
OCI_ConnectionEnableServerOutput EnableServerOutput allocate the buffer (bufsize, arrsize, linesize)
OCI_ConnectionGetServerOutput GetServerOutput pop the next buffered line (NULL/empty when drained)
OCI_ConnectionDisableServerOutput DisableServerOutput turn it off
OCI_ConnectionEnableServerOutput(cn, 32000, 5, 255);   /* buf 32k, 5 lines/batch, ≤255 chars */

OCI_StatementExecute(st, OTEXT(
    "begin "
    "  dbms_output.put_line('First  line'); "
    "  dbms_output.put_line('Second line'); "
    "end;"));

const otext *line;
while ((line = OCI_ConnectionGetServerOutput(cn)))
{
    printf("%s\n", line);
}

OCI_ConnectionDisableServerOutput(cn);

Flow

  1. Enable output before executing the PL/SQL that prints.
  2. Execute — put_line writes go into the buffer.
  3. Read the buffer after the call returns — lines arrive FIFO; drain fully.
  4. Repeat for more calls, or disable when done.

Sizing

Putting it together — the typical sequence

DBMS_OUTPUT is PL/SQL's print — OCILIB drains it like this:

  1. Enable it — OCI_ServerEnableOutput(con, size, bufsize, linesize) allocates the server-side buffer for the session.
  2. Run the PL/SQL — any DBMS_OUTPUT.PUT_LINE inside lands in the buffer.
  3. Drain it — OCI_ServerGetOutput returns the buffered text as one otext* (or empty when there's nothing).
  4. Disable — OCI_ServerDisableOutput frees the buffer.

Hints: enable before execute — output buffered while disabled is lost forever, not queued; the buffer is per-connection, not per-statement — two PL/SQL calls append to the same drain; and GetOutput is how you read the buffer — calling it mid-run gets whatever's been PUT_LINE'd so far, not the whole run's output.

Try it yourself

  1. Print a progress trace from a multi-statement block (put_line between steps).
  2. Set linesize to 10 and watch long lines wrap.
  3. Use GetServerOutput to build a "log pane" around your qtool from Checkpoint 1.

Common pitfalls

Recap

Checkpoint: Part 4 ends with a small payroll-procedure exercise.