Skip to main content
← PL/SQL Topics

SQL Integration

Explain calling a PL/SQL function defined in the WITH clause of a query

The WITH FUNCTION Clause

ExplanationOracle Fusion / EBS · Technical · PL/SQLHigh confidenceGenerated

WITH FUNCTION declares a PL/SQL function inside the query itself, cutting the SQL-to-PL/SQL context-switch cost.

How it works

Since 12.1 a SELECT can define a function in its WITH clause: WITH FUNCTION f(...) RETURN ... IS BEGIN ... END; SELECT f(col) FROM ... . The function is visible only to that statement and runs with less context-switch overhead than a stored function — useful for a one-off transformation in a report. The statement must end with the slash-terminated style in tools that otherwise treat ';' as the terminator. A stored function can get similar speed with the UDF pragma.

How it works
WITH
  FUNCTION annual_cost(p_monthly IN NUMBER, p_load_pct IN NUMBER) RETURN NUMBER IS
  BEGIN
    RETURN ROUND(p_monthly * 12 * (1 + p_load_pct / 100), 2);
  END;
SELECT employee_id,
       annual_cost(salary, 30) AS fully_loaded_annual
FROM   employees
WHERE  department_id = 30;
/

Related questions

GroundingGenerated

Model-generated, grounded against the curated knowledge layer. Check specifics against your instance.

Have a follow-up, or a different question?

Continue in Ask Oracle AI