Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1 views

Chapter 13 How to create Store Procedures and Functions Lab 1

Uploaded by

sak471725
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Chapter 13 How to create Store Procedures and Functions Lab 1

Uploaded by

sak471725
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

ROYAL UNIVERSITY OF PHNOM PENH

DATABASE II

Chapter 13
How to create stored procedures and functions (Lab)

Lecturer: Mr. Chhim Bunchhun,


chhim.bunchhun@rupp.edu.kh, 093 222 380
A stored procedure that displays a message

DELIMITER //
CREATE PROCEDURE test()
BEGIN
SELECT 'This is a test.' AS message;
END//

CALL test();

The response from the system

Chapter 13 How to create stored procedures and functions 2


A script that creates and calls 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;

Chapter 13 How to create stored procedures and functions 3


A script that creates and calls a stored procedure (continued)

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();

The response from the system

Chapter 13 How to create stored procedures and functions 4


A stored procedure that uses variables
DELIMITER //

CREATE PROCEDURE test()


BEGIN
DECLARE max_invoice_total DECIMAL(9,2);
DECLARE min_invoice_total DECIMAL(9,2);
DECLARE percent_difference DECIMAL(9,4);
DECLARE count_invoice_id INT;
DECLARE vendor_id_var INT;

SET vendor_id_var = 95;

SELECT MAX(invoice_total), MIN(invoice_total),


COUNT(invoice_id)
INTO max_invoice_total, min_invoice_total,
count_invoice_id
FROM invoices WHERE vendor_id = vendor_id_var;

Chapter 13 How to create stored procedures and functions 5


A stored procedure that uses variables (cont.)

SET percent_difference =
(max_invoice_total - min_invoice_total) /
min_invoice_total * 100;

SELECT CONCAT('$', max_invoice_total) AS 'Maximum invoice',


CONCAT('$', min_invoice_total) AS 'Minimum invoice',
CONCAT('%', ROUND(percent_difference, 2)) AS 'Percent difference',
count_invoice_id AS 'Number of invoices';
END//

The response from the system

Chapter 13 How to create stored procedures and functions 6


A stored procedure that uses an IF statement
DELIMITER //

CREATE PROCEDURE test()


BEGIN
DECLARE first_invoice_due_date DATE;

SELECT MIN(invoice_due_date)
INTO first_invoice_due_date
FROM invoices
WHERE invoice_total - payment_total - credit_total > 0;

IF first_invoice_due_date < NOW() THEN


SELECT 'Outstanding invoices are overdue!';
ELSEIF first_invoice_due_date = NOW() THEN
SELECT 'Outstanding invoices are due today!';
ELSE
SELECT 'No invoices are overdue.';
END IF;
END//

The response from the system

Chapter 13 How to create stored procedures and functions 7


A stored procedure with a simple CASE statement
DELIMITER //

CREATE PROCEDURE test()


BEGIN
DECLARE terms_id_var INT;

SELECT terms_id INTO terms_id_var


FROM invoices WHERE invoice_id = 4;

CASE terms_id_var
WHEN 1 THEN
SELECT 'Net due 10 days' AS Terms;
WHEN 2 THEN
SELECT 'Net due 20 days' AS Terms;
WHEN 3 THEN
SELECT 'Net due 30 days' AS Terms;
ELSE
SELECT 'Net due more than 30 days' AS Terms;
END CASE;
END//

The response from the system

Chapter 13 How to create stored procedures and functions 8


A stored procedure that uses a WHILE loop

DELIMITER //

CREATE PROCEDURE test()


BEGIN
DECLARE i INT DEFAULT 1;
DECLARE s VARCHAR(400) DEFAULT '';

WHILE i < 4 DO
SET s = CONCAT(s, 'i=', i, ' | ');
SET i = i + 1;
END WHILE;

SELECT s AS message;

END//

The output for this code

Chapter 13 How to create stored procedures and functions 9


A stored procedure that uses a cursor

DELIMITER //

CREATE PROCEDURE test()


BEGIN
DECLARE invoice_id_var INT;
DECLARE invoice_total_var DECIMAL(9,2);
DECLARE row_not_found TINYINT DEFAULT FALSE;
DECLARE update_count INT DEFAULT 0;

DECLARE invoices_cursor CURSOR FOR


SELECT invoice_id, invoice_total FROM invoices
WHERE invoice_total - payment_total - credit_total > 0;

DECLARE CONTINUE HANDLER FOR NOT FOUND


SET row_not_found = TRUE;

OPEN invoices_cursor;

WHILE row_not_found = FALSE DO


FETCH invoices_cursor INTO invoice_id_var, invoice_total_var;

Chapter 13 How to create stored procedures and functions 10


A stored procedure that uses a cursor (continued)
IF invoice_total_var > 1000 THEN
UPDATE invoices
SET credit_total = credit_total + (invoice_total * .1)
WHERE invoice_id = invoice_id_var;
SET update_count = update_count + 1;
END IF;
END WHILE;

CLOSE invoices_cursor;

SELECT CONCAT(update_count, ' row(s) updated.');


END//

The response from the system

Chapter 13 How to create stored procedures and functions 11


THANK YOU
for your attention !

You might also like