Oracle: Advanced PL/SQL: Real-World
Module: Database-Specific Features
Advanced PL/SQL is essential for high-performance Oracle applications. Oracle ERP systems use bulk operations to process 1M+ records in minutes instead of hours. Reporting systems use dynamic SQL for flexible user-defined queries. Telecom companies use collections to process millions of CDR records. Data warehouses use pipelined functions for memory-efficient ETL. Understanding advanced PL/SQL separates senior developers from junior developers.
Oracle ERP Nightly Batch Processing
**Company**: Large Enterprise using Oracle ERP
**Challenge**: Process 1M+ orders in nightly batch. Each order requires status update, inventory adjustment, and accounting entry. Row-by-row processing takes 8 hours (batch window is 6 hours). Batch fails nightly, causing operational issues.
**Solution**: Rewrite using FORALL and BULK COLLECT. Process 10,000 orders per batch. Bulk operations reduce context switches from 1M to 100. Error handling with SAVE EXCEPTIONS continues processing after errors.
**Results**: Batch time reduced from 8 hours to 5 minutes (96x faster). Batch completes within window. Error handling processes 99.9% of orders (errors logged for manual review). Memory usage constant (10K rows per batch). Zero operational issues in 2 years.
-- Batch processing with bulk operations
CREATE OR REPLACE PROCEDURE process_orders_batch AS
TYPE t_order_ids IS TABLE OF NUMBER;
v_order_ids t_order_ids;
c_batch_size CONSTANT PLS_INTEGER := 10000;
BEGIN
LOOP
SELECT order_id BULK COLLECT INTO v_order_ids
FROM orders WHERE status = 'PENDING'
FETCH FIRST c_batch_size ROWS ONLY;
EXIT WHEN v_order_ids.COUNT = 0;
FORALL i IN 1..v_order_ids.COUNT SAVE EXCEPTIONS
UPDATE orders SET status = 'PROCESSED'
WHERE order_id = v_order_ids(i);
COMMIT;
END LOOP;
END;
/
-- Performance: 1M orders in 5 minutes (vs 8 hours row-by-row)
All
Dynamic Reporting System
**Company**: Business Intelligence Platform
**Challenge**: Users create custom reports with dynamic columns, filters, and aggregations. Static SQL cannot handle 1000+ report variations. Need flexible query generation while preventing SQL injection.
**Solution**: EXECUTE IMMEDIATE with bind variables for dynamic SQL. Validate table/column names against metadata. REF CURSOR for dynamic result sets. Query builder UI generates safe SQL.
**Results**: 1000+ report variations supported. Zero SQL injection incidents in 3 years. Query generation time < 100ms. Users create reports without developer involvement. Platform serves 10K+ users.
-- Dynamic report generator
CREATE OR REPLACE PROCEDURE generate_report(
p_table VARCHAR2,
p_columns VARCHAR2,
p_filter_col VARCHAR2,
p_filter_val VARCHAR2
) AS
v_sql VARCHAR2(4000);
v_cursor SYS_REFCURSOR;