Stored Routines
Stored Routines
//Function body
END;
….
Example:
A function to compute the total mark based on a coursework mark
and exam mark: Coursework=30% and Exam =70%
DELIMITER $$
CREATE FUNCTION totalMark(courseWorkMark INT, examMark INT)
RETURNS INT
BEGIN
RETURN courseWorkMark*0.3 + examMark*0.7;
END $$
DELIMITER $$
DROP FUNCTION IF EXISTS ageFinder $$
CREATE FUNCTION ageFinder (dob DATE) RETURNS INT
BEGIN
RETURN TRUNCATE((DATEDIFF(SYSDATE(), dob)/365),0);
END $$
DELIMITER $$
CREATE FUNCTION commentGenerator(mark INT) RETURNS CHAR(45)
BEGIN
IF mark>=50 THEN
RETURN ‘Excelllent’;
ELSE
RETURN ‘Improvement Needed’;
END IF;
END $$
IF mark>=80 THEN
RETURN ‘A’;
ELSEIF mark>=75 THEN
RETURN ‘B+’;
ELSEIF mark>=70 THEN
RETURN ‘B’;
ELSEIF mark>=60 THEN
RETURN ‘C+’;
ELSEIF mark>=55 THEN
RETURN ‘C’;
ELSEIF mark>=50 THEN
RETURN ‘C-’;
ELSE
RETURN ‘D’;
END IF;
END $$
Example 3: Calculations:
Computing the PAYE based on the conditions:
IF GrossPay<235,000 – Paye = 0
IF GrossPay between 235,000 and 410,000, Paye = 10%
IF GrossPay is above 410,000, Paye = 30%
DELIMITER $$
CREATE FUNCTION payeCalculator(GrossPay INT) RETURNS INT
BEGIN
IF GrossPay<235000 THEN
RETURN 0;
ELSEIF GrossPay BETWEEN 235000 AND 410000 THEN
RETURN 0.1 * GrossPay;
ELSE
RETURN 0.3 * GrossPay;
END IF;
END $$
…
Nested IF statements:
These involve multiple layers of IF statements.
Example:
If a resident is from Kampala:
Taxes are computed as follows: If income<130,000 no tax else 20% tax;
If resident is from Northern Uganda
Taxes are computed as follows: If income<100,000 no tax else 15% tax;
All other areas:
Taxes are computed as follows: If income<150,000 no tax else 25% tax;
DELIMITER $$
CREATE FUNCTION taxByRegion(region CHAR(35), income INT) RETURNS INT
BEGIN
If region=‘Kampala’ THEN
If income < 130,000 THEN
RETURN 0;
ELSE
RETURN 0.2 * income;
END IF;
ELSEIF region=‘Northen’ THEN
If income < 100,000 THEN
RETURN 0;
ELSE
RETURN 0.1 * income;
END IF;
ELSE
If income < 150,000 THEN
RETURN 0;
ELSE
RETURN 0.25 * income;
END IF;
END IF;
END $$
…
DELIMITER $$
CREATE FUNCTION hallAssigner(gender CHAR(10), age INT) RETURNS CHAR(25)
BEGIN
IF gender=‘M’ THEN
IF age > 21 THEN
RETURN ‘ABUBAKAR’;
ELSE
RETURN ‘UTHMAN’;
END IF;
ELSE
IF age > 21 THEN
RETURN ‘SUMAYYAH’;
ELSE
RETURN ‘KASWAMPA’;
END IF;
END IF;
END $$
DELIMITER;
…
Using CURSORS:
Cursors are important when the function needs to obtain data to be used
in the calculation from an existing table.
EXAMPLE
To Create a function that gives a comment based on the total purchases
done so far:
IF purchases are less than 3 million – “Sustainable” else “Unsustainble”
The total value of purchases must be obtained from the purchases table
before being used in making the comment.
TASK
To Create a function that gives a comment based on the total purchases
done so far:
purchases are LESS than average purchases – “Low”
purchases are EQUAL to average purchases – “Average”
purchases are MORE than average purchases – “High”
The total value of purchases must be obtained from the purchases table
before being used in making the comment.