If you haven't already done so, I would suggest reading Introduction To Stored Procedures and Functions Part 1, for a better understanding.
Part 2
There are two types of functions that MySQL allows you to create. A stored function and a User Defined Function (UDF). Although the two are created using the CREATE FUNCTION statement and share the same namespace, they are both implemented differently. The focus of this article is to explain store functions.A function is much like a procedure, in that it is a routine stored on the server and like a procedure, it too can accept parameters. However functions can't return more than one value nor can they return resultsets. They are used to return a single value when used in an SQL statement. Stored functions only have an IN parameter mode, the function body is responsible for returning a value. Unlike stored procedures which need to be invoked using the CALL statement, functions are invoked within SQL statements like built in functions. The example code below creates a stored function that returns an uppercase string. The function simply uses the built-in UPPER function do demonstrate this.
- DELIMITER $$
- CREATE FUNCTION toupper(str LONGTEXT)
- RETURNS LONGTEXT
- RETURN UPPER(str);
- SELECT TOUPPER('hello world')
- # Outputs HELLO WORLD
Looking at the CREATE FUNCTION statement, you will notice, that there are two statements RETURNS and RETURN. The RETURNS statement simply specifies the return data type while the RETURN statement returns a value. Also notice that the BEING AND END statements have been ommited. This is because the function body is contained within a single line.
Now, let's take a look at a few useful stored functions that are easy to create. The next code example demonstrates how to create a function that can be used to change the first character in a string to uppercase. This function is equivalent to PHP's ucfirst().
- CREATE FUNCTION uc_first(str LONGTEXT)
- RETURNS LONGTEXT
- BEGIN
- SET @firstChar = SUBSTRING(str, 1,1);
- SET @remainingChars = SUBSTRING(str, 2);
- RETURN CONCAT(UPPER(@firstChar), @remainingChars);
- END
The code above uses three of MySQL's built-in string functions. SUBSTRING, CONCAT and UPPER. As the name suggests, the SUBSTRING function returns a substring from a larger string. In this case the first character is returned and placed into a variable @firstChar, while the remaining string are stored in the @remainingChars variable. The two variables are then concatenated but @firstChar is turned into uppercase before the final string is returned.
This next function converts the first character in a word sentence to uppercase. While at first glance the function may seem complicated, a deeper inspection of the code will reveal its simplicity.
- CREATE FUNCTION uc_words(str LONGTEXT)
- RETURNS LONGTEXT
- BEGIN
- DECLARE returnStr LONGTEXT DEFAULT '';
- DECLARE len INT DEFAULT 0;
- DECLARE i INT DEFAULT 1;
- DECLARE ch CHAR(1);
- DECLARE m bool DEFAULT true;
- SET len = LENGTH(str) + 1;
- WHILE (i < len) DO
- BEGIN
- SET ch = SUBSTRING(str, i, 1);
- IF m = true THEN
- BEGIN
- SET returnStr = CONCAT(returnStr, UCASE(ch));
- SET m = FALSE;
- END;
- ELSEIF ch = ' ' THEN
- BEGIN
- SET returnStr = CONCAT(returnStr, ' ');
- SET m = TRUE;
- END;
- ELSE
- BEGIN
- SET returnStr = CONCAT(returnStr, ch);
- END;
- END IF;
- SET i = i+1;
- END;
- END WHILE;
- RETURN returnStr;
- END
- SELECT uc_words('the quick brown fox jumps over the lazy dog')
The final function in this article, although not very accurate will return the number of years elapsed between a given date and the current date. The function is named age as it can be used to determine how old a person is or will be at the current year defined by the NOW() function.
- CREATE FUNCTION age(dob DATETIME)
- RETURNS INT
- BEGIN
- RETURN DATEDIFF(NOW(), dob) / 365;
- END
- SELECT age('1970-05-22')
Hopefully the examples in this article have given you an idea as to what stored functions are and how they are used.
No comments:
Post a Comment