First page Back Continue Last page Image

Example: Dynamic Product of the Last Procedure

SELECT invoice_id, invoice_number, invoice_date, invoice_total

FROM invoices

WHERE invoice_date > '2017-07-25' AND invoice_total > 100

DELIMITER //

CREATE PROCEDURE select_invoices

( min_invoice_date_param DATE,

min_invoice_total_param DECIMAL(9,2))

BEGIN

DECLARE select_clause VARCHAR(200);

DECLARE where_clause VARCHAR(200);

SET select_clause = "SELECT invoice_id, invoice_number, invoice_date, invoice_total FROM invoices ";

SET where_clause = "WHERE ";

IF min_invoice_date_param IS NOT NULL THEN

SET where_clause = CONCAT(where_clause,

" invoice_date > '", min_invoice_date_param, "'");

END IF;

IF min_invoice_total_param IS NOT NULL THEN

IF where_clause != "WHERE " THEN

SET where_clause = CONCAT(where_clause, "AND ");

END IF;

SET where_clause = CONCAT(where_clause,

"invoice_total > ", min_invoice_total_param);

END IF;