OCILIB Tutorials v5.0.0
Intermediate

Describing SQL without executing it

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

Concepts

Describing a statement asks the server to parse the SQL and hand back the shape of its result — columns, types, sizes — without executing a single row:

OCI_StatementDescribe(st, OTEXT("select empno, ename, sal from employees where deptno = :d"));

OCI_Resultset *rs = OCI_StatementGetResultset(st);     /* metadata-only resultset */
for (OCI_UInt32 i = 0; i < OCI_ResultsetGetColumnCount(rs); ++i)
{
    OCI_Column *col = OCI_ResultsetGetColumn(rs, i);
    printf("%-15s %s\n", OCI_ColumnGetName(col), OCI_ColumnGetSqlType(col));
}

Parse vs Describe vs Prepare

C call C++ method Server parse Exec plan Column metadata Rows
OCI_StatementParse Parse ✓ — — —
OCI_StatementDescribe Describe ✓ ✓ ✓ —
OCI_StatementPrepare + OCI_StatementExecutePrepared Prepare + ExecutePrepared ✓ ✓ ✓ ✓

Where it shines

Putting it together — the typical sequence

Describing asks the server "what is this object" — the schema-introspection path:

  1. Call OCI_Describe with the object name — table, view, procedure, sequence, type.
  2. Walk the returned metadata: column list, types, sizes, nullability — the same OCI_Column attributes as a resultset describe.
  3. Use it for code generation or dynamic SQL — any tool that adapts to schema at runtime starts here.

Hints: describe hits the data dictionary — cache the result if you call it in a loop; OCI_TIF_TABLE vs OCI_TIF_TYPE selects table-vs-type — the same function describes both; and describe answers "what the schema says", while resultset metadata answers "what this query returned" — pick the right one for the question you're asking.

Try it yourself

  1. Describe select * from employees and render a sqlplus-style header.
  2. Describe an insert statement — what does the column count look like?
  3. Compare OCI_StatementParse vs OCI_StatementDescribe on a syntactically wrong query — which one surfaces OCI_StatementGetSqlErrorPos?

Common pitfalls

Recap

Next: making large fetches fast — fetch size, prefetch, long mode.