Beginnerdemo/output.c
DBMS_OUTPUT — server-side print capture
Objectives — by the end of this page you will be able to:
- enable and drain
DBMS_OUTPUTfrom OCILIB - tune buffer sizes for chatty PL/SQL
- know when it's useful (and when it isn't)
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);
con.EnableServerOutput(32000, 5, 255);
st.Execute("begin dbms_output.put_line('First line');"
" dbms_output.put_line('Second line'); end;");
std::vector<String> lines;
while (con.GetServerOutput(lines)) /* batch-fills lines */
{
for (auto &l : lines) std::cout << l << "\n";
}
con.DisableServerOutput();
Flow
- Enable output before executing the PL/SQL that prints.
- Execute —
put_linewrites go into the buffer. - Read the buffer after the call returns — lines arrive FIFO; drain fully.
- Repeat for more calls, or disable when done.
Sizing
bufsize— total server buffer (default Oracle limit is fine for learning).arrsize— lines fetched per round-trip.linesize— max chars per line (Oracle caps at 32767 in recent versions; 255 is the classic default).
Putting it together — the typical sequence
DBMS_OUTPUT is PL/SQL's print — OCILIB drains it like this:
- Enable it —
OCI_ServerEnableOutput(con, size, bufsize, linesize)allocates the server-side buffer for the session. - Run the PL/SQL — any
DBMS_OUTPUT.PUT_LINEinside lands in the buffer. - Drain it —
OCI_ServerGetOutputreturns the buffered text as oneotext*(or empty when there's nothing). - Disable —
OCI_ServerDisableOutputfrees 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
- Print a progress trace from a multi-statement block (
put_linebetween steps). - Set
linesizeto 10 and watch long lines wrap. - Use
GetServerOutputto build a "log pane" around yourqtoolfrom Checkpoint 1.
Common pitfalls
- Getting NULL immediately — you enabled output after the execute, or forgot to drain until NULL before the next call.
- Expecting real-time output — lines only arrive after the call completes (not during a long-running block).
- Production use — it's a debugging channel, not a data API; prefer logging tables for anything durable.
Recap
- Enable → execute → drain the buffer → disable.
- A debugging lifeline for PL/SQL — but not a streaming channel.
Checkpoint: Part 4 ends with a small payroll-procedure exercise.