LATERAL "(" <procedure_name> "(" <table_name>.<column_name> ")" ")" AS <correlation_name> Here is an example of a procedure that receives the customer id as an argument and returns a result set containing all the sales order information for that customer: CREATE PROCEDURE p_customer_orders ( IN @customer_id INTEGER ) BEGIN MESSAGE STRING ( 'DIAG ', CURRENT TIMESTAMP, ' ', @customer_id ) TO CONSOLE; SELECT sales_order.order_date AS order_date, product.name AS product_name, product.description AS description, sales_order_items.quantity AS quantity, product.unit_price * sales_order_items.quantity AS amount FROM sales_order INNER JOIN sales_order_items ON sales_order_items.id = sales_order.id INNER JOIN product ON product.id = sales_order_items.prod_id WHERE sales_order.cust_id = @customer_id ORDER BY order_date, product_name, description; END;
CALL p_customer_orders ( 141 );
Here is the result of the CALL for customer id 141, using the ASADEMO database: order_date product_name description quantity amount ========== ============ ============= ======== ====== 2000-11-19 Shorts Cotton Shorts 36 540.00 2001-02-26 Baseball Cap Cotton Cap 12 108.00 The following is an example where that procedure is called in a FROM clause in a select that specifies the company name, Mall Side Sports, instead of the customer id 141. The customer table is joined to the procedure call with the comma join operator, and the procedure call is called as part of a LATERAL derived table definition, because the customer.id column is passed as an argument. SELECT customer.company_name, customer_orders.* FROM customer, LATERAL ( p_customer_orders ( customer.id ) ) AS customer_orders WHERE customer.company_name = 'Mall Side Sports' ORDER BY customer_orders.order_date, customer_orders.product_name, customer_orders.description; Here is the final result; same data as before, plus the company name: company_name order_date product_name description quantity amount ================ ========== ============ ============= ======== ====== Mall Side Sports 2000-11-19 Shorts Cotton Shorts 36 540.00 Mall Side Sports 2001-02-26 Baseball Cap Cotton Cap 12 108.00