First page Back Continue Last page Image

An Example of a Stored Procedure

USE ap; 

DROP PROCEDURE IF EXISTS test;

 

-- Change statement delimiter from semicolon to double front slash

DELIMITER //

 

CREATE PROCEDURE test()

BEGIN

DECLARE sum_balance_due_var DECIMAL(9, 2);

 

SELECT SUM(invoice_total - payment_total - credit_total)

INTO sum_balance_due_var

FROM invoices

WHERE vendor_id = 95;

IF sum_balance_due_var > 0 THEN

SELECT CONCAT('Balance due: $', sum_balance_due_var) AS message;

ELSE

SELECT 'Balance paid in full' AS message;

END IF;

END// 

-- Change statement delimiter from double front slash to semicolon

DELIMITER ;

 

CALL test();

Used to replace an existing Procedure

Used to change the functionality of the semicolon

Defines and Names the new Procedure

Defines Beginning and End of the Procedure

Used to reset the functionality of the semicolon

Run the Procedure