Determining the Code of an stored procedures : Introduction « Procedure Function « SQL / MySQL






Determining the Code of an stored procedures

       


mysql>
mysql> delimiter $$
mysql>
mysql>
mysql> CREATE FUNCTION shorten(s VARCHAR(255), n INT)
    ->  RETURNS VARCHAR(255)
    ->  BEGIN
    ->     IF ISNULL(s) THEN
    ->        RETURN '';
    ->     ELSEIF n<15 THEN
    ->        RETURN LEFT(s, n);
    ->     ELSE IF CHAR_LENGTH(s) <= n THEN
    ->            RETURN s;
    ->         ELSE
    ->            RETURN CONCAT(LEFT(s, n-10), ' ... ', RIGHT(s, 5));
    ->         END IF;
    ->     END IF;
    ->  END$$
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> SELECT shorten("abcdefghijklmnopqrstuvwxyz", 15)$$
+-------------------------------------------+
| shorten("abcdefghijklmnopqrstuvwxyz", 15) |
+-------------------------------------------+
| abcde ... vwxyz                           |
+-------------------------------------------+
1 row in set (0.00 sec)

mysql>
mysql> delimiter ;
mysql>
mysql> SHOW CREATE FUNCTION shorten;
+----------+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---
-------------------+----------------------+--------------------+
| Function | sql_mode | Create Function                                                                                                                                                                                                                                                                                                                                                                     | ch
aracter_set_client | collation_connection | Database Collation |
+----------+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---
-------------------+----------------------+--------------------+
| shorten  |          | CREATE DEFINER=`root`@`localhost` FUNCTION `shorten`(s VARCHAR(255), n INT) RETURNS varchar(255) CHARSET latin1
BEGIN
    IF ISNULL(s) THEN
       RETURN '';
    ELSEIF n<15 THEN
       RETURN LEFT(s, n);
    ELSE IF CHAR_LENGTH(s) <= n THEN
           RETURN s;
        ELSE
           RETURN CONCAT(LEFT(s, n-10), ' ... ', RIGHT(s, 5));
        END IF;
    END IF;
 END | latin1               | latin1_swedish_ci    | latin1_swedish_ci  |
+----------+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---
-------------------+----------------------+--------------------+
1 row in set (0.00 sec)

mysql>
mysql>
mysql> drop function shorten;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>

   
    
    
    
    
    
    
  








Related examples in the same category

1.Using user-defined function in a select statement
2.Use user-defined function in a select statement to deal with data in a table
3.Use user-defined function in order by clause
4.Use user-defined function in where clause
5.Recursion
6.Using aggregate function in procedure
7.Create procedure for definer