The "WITH FUNCTION" clause, introduced in Oracle Database 12c, represents a significant enhancement for SQL developers working with Oracle Fusion Applications. It allows for the definition of PL/SQL functions directly within the WITH clause of a SQL query. This capability empowers users to create more readable, maintainable, and often more performant queries by encapsulating complex logic locally within a single SQL statement, rather than requiring standalone stored functions. This is particularly beneficial in the context of Oracle Fusion Applications – encompassing Human Capital Management (HCM), Supply Chain Management (SCM), and Enterprise Resource Planning (ERP) – and its powerful reporting tool, BI Publisher.
The "WITH FUNCTION" clause is a feature of Oracle SQL that allows you to define PL/SQL functions inline within the WITH clause of a query. This means the function's scope is limited to the query in which it is defined, promoting modularity and encapsulation of logic.
Prior to Oracle Database 12c, if you needed custom PL/SQL logic within a SQL query, you typically had to create a stored function in the database schema. The WITH FUNCTION syntax allows you to embed this logic directly:
WITH
FUNCTION function_name (parameter1 datatype, parameter2 datatype, ...)
RETURN return_datatype
IS
-- Declaration section (optional)
BEGIN
-- PL/SQL logic
RETURN result_value;
END;
SELECT
column1,
function_name(column_x) AS calculated_value
FROM
your_table
WHERE
condition;
This approach makes the SQL statement self-contained and easier to manage, especially for complex, ad-hoc queries or reports where creating permanent schema objects might be overkill.
Employing "WITH FUNCTION" offers several advantages:
Oracle Fusion Applications, built on top of the Oracle Database, can fully leverage the "WITH FUNCTION" clause in various scenarios across its modules. Custom reporting, data extractions, and complex data validations are common areas where this feature proves invaluable.
Visual representation of data flow and extraction in Oracle Fusion Cloud, where advanced SQL like "WITH FUNCTION" can be applied.
In Oracle Fusion HCM, "WITH FUNCTION" can be used for sophisticated employee data analysis and reporting. For example:
WITH
FUNCTION calculate_service_band (p_hire_date DATE)
RETURN VARCHAR2
IS
v_years_of_service NUMBER;
BEGIN
v_years_of_service := MONTHS_BETWEEN(SYSDATE, p_hire_date) / 12;
IF v_years_of_service < 1 THEN
RETURN 'Less than 1 Year';
ELSIF v_years_of_service < 5 THEN
RETURN '1-5 Years';
ELSIF v_years_of_service < 10 THEN
RETURN '5-10 Years';
ELSE
RETURN '10+ Years';
END IF;
END;
SELECT
employee_number,
full_name,
hire_date,
calculate_service_band(hire_date) AS service_band
FROM
per_all_people_f papf -- Example HCM table
WHERE
papf.person_id = :person_id_param;
Oracle Fusion SCM often involves intricate calculations for inventory, logistics, and order management. "WITH FUNCTION" can simplify these:
WITH
FUNCTION get_inventory_status (p_on_hand_qty NUMBER, p_reorder_point NUMBER)
RETURN VARCHAR2
IS
BEGIN
IF p_on_hand_qty <= p_reorder_point THEN
RETURN 'Reorder Needed';
ELSIF p_on_hand_qty <= (p_reorder_point * 1.2) THEN
RETURN 'Low Stock';
ELSE
RETURN 'Sufficient Stock';
END IF;
END;
SELECT
item_number,
on_hand_quantity,
reorder_point,
get_inventory_status(on_hand_quantity, reorder_point) AS inventory_alert
FROM
inv_onhand_quantities_detail ioqd -- Example SCM table
WHERE
ioqd.organization_id = :org_id_param;
In Oracle Fusion ERP, particularly within financials, "WITH FUNCTION" can assist in complex general ledger reporting, subledger accounting transformations, or custom financial calculations:
WITH
FUNCTION calculate_discounted_amount (p_invoice_amount NUMBER, p_payment_terms_id NUMBER)
RETURN NUMBER
IS
v_discount_rate NUMBER := 0;
BEGIN
-- Simplified logic: Fetch discount rate based on payment terms
IF p_payment_terms_id = 1001 THEN -- e.g., '2% 10 Net 30'
v_discount_rate := 0.02;
ELSIF p_payment_terms_id = 1002 THEN -- e.g., '1% 15 Net 45'
v_discount_rate := 0.01;
END IF;
RETURN p_invoice_amount * (1 - v_discount_rate);
END;
SELECT
invoice_num,
invoice_amount,
payment_terms_id,
calculate_discounted_amount(invoice_amount, payment_terms_id) AS net_payable_after_discount
FROM
ap_invoices_all aia -- Example ERP table
WHERE
aia.vendor_id = :vendor_id_param;
Oracle BI Publisher is the strategic reporting solution for Oracle Fusion Applications. It uses data models, typically SQL-based, to fetch data for reports. The "WITH FUNCTION" clause can be directly embedded into these SQL queries within BI Publisher data models, enabling sophisticated data manipulation and formatting at the data retrieval stage.
When designing a data model in BI Publisher:
WITH FUNCTION clause as needed.This approach is particularly useful for:
WITH
FUNCTION format_order_status (p_status_code VARCHAR2)
RETURN VARCHAR2
IS
BEGIN
IF p_status_code = 'AWAIT_SHIP' THEN
RETURN 'Awaiting Shipment';
ELSIF p_status_code = 'SHIPPED' THEN
RETURN 'Shipped - In Transit';
ELSIF p_status_code = 'DELIVERED' THEN
RETURN 'Delivered to Customer';
ELSE
RETURN 'Status Unknown';
END IF;
END;
SELECT
h.order_number,
l.line_number,
i.item_description,
l.ordered_quantity,
format_order_status(l.flow_status_code) AS user_friendly_status -- Using the inline function
FROM
doo_headers_all h,
doo_lines_all l,
egp_system_items_b i
WHERE
h.header_id = l.header_id
AND l.inventory_item_id = i.inventory_item_id
AND h.submitted_date >= :p_start_date
AND h.submitted_date <= :p_end_date;
In this BI Publisher data model, the format_order_status function translates internal status codes into more readable descriptions for the report output. This logic is neatly encapsulated within the SQL query.
This mindmap illustrates how the "WITH FUNCTION" SQL feature integrates within the Oracle Fusion Applications landscape, touching upon HCM, SCM, ERP modules, and its specific utility in BI Publisher reporting. It highlights common use cases and benefits across these areas.
The following radar chart provides an opinionated assessment of the impact of using the "WITH FUNCTION" clause across different aspects like Readability, Maintainability, Performance, Complexity Reduction, and Reusability within Oracle Fusion HCM, SCM, ERP modules, and BI Publisher reporting. The scale represents perceived benefit, where a higher score indicates a greater positive impact. These are general assessments and can vary based on specific use cases.
*Performance benefits are use-case specific and should be tested. While inline functions can reduce context switching, very complex PL/SQL logic might still perform better in pre-compiled stored functions.
Implementing "WITH FUNCTION" requires access to a SQL interface connected to your Oracle Fusion Applications database (e.g., SQL Developer, BI Publisher Data Model editor) and an understanding of the underlying data structures.
Construct your SQL query, starting with the WITH clause to define your function(s). You can define multiple functions, each followed by a comma, before defining any Common Table Expressions (CTEs) or the main SELECT statement.
WITH
FUNCTION function_name1 (params...) RETURN datatype IS BEGIN ... END;, -- Note the comma for multiple functions/CTEs
FUNCTION function_name2 (params...) RETURN datatype IS BEGIN ... END; -- Last function does not need a comma before main SELECT
SELECT ...
:p_start_date) that will be passed to the report.WITH clause are only visible within that specific SQL statement. They cannot be called from outside.EXPLAIN PLAN to understand the execution path.WITH clause cannot call other functions or procedures that are not also defined within the same WITH clause or are standard built-in SQL/PLSQL functions. They generally cannot invoke packaged procedures/functions or perform DML operations that commit.The following video discusses enhancements to Oracle SQL's WITH clause and SQL macros, which are related concepts for writing powerful and maintainable SQL. Understanding these broader capabilities can provide context for leveraging features like "WITH FUNCTION" effectively.
"How to write powerful SQL that's easy to maintain" by Oracle, discussing WITH clause enhancements.
This table summarizes key aspects of implementing "WITH FUNCTION" within Oracle Fusion modules and BI Publisher, highlighting example use cases and primary benefits.
| Area | Example Use Case | Primary Benefit | Typical Implementation Context |
|---|---|---|---|
| Oracle Fusion HCM | Calculating dynamic employee eligibility for programs based on multiple criteria (e.g., tenure, grade, location). | Encapsulates complex HR business rules directly within data extraction queries for reports or analytics. | BI Publisher reports, custom data extracts, HCM analytics. |
| Oracle Fusion SCM | Determining custom inventory valuation or categorization (e.g., fast-moving vs. slow-moving) inline. | Simplifies complex SCM logic within queries for dashboards or operational reports without needing separate DB objects. | BI Publisher reports, SCM dashboards, ad-hoc supply chain analysis. |
| Oracle Fusion ERP | Applying specific financial period closing adjustments or multi-step allocation logic in a query. | Enhances financial reporting by embedding sophisticated calculation logic directly into data models. | BI Publisher financial statements, custom ERP reports, period-end reconciliation queries. |
| BI Publisher (General) | Formatting data (e.g., complex date/string manipulations) or deriving calculated fields that depend on conditional logic across multiple columns. | Reduces complexity in report templates (RTF, XPT) by handling data transformations at the SQL level, improving maintainability and often performance. | SQL-based Data Models in BI Publisher across all Fusion pillars. |