-1

I have been studying about MySQL functions. while studying about the functions i saw the function named "MySQL Stored Function".

Here is Example that i don't understand this two thing. One is

SELECT 
    customerName, CustomerLevel(creditLimit)
FROM
    customers
ORDER BY customerName;

Second one is

DELIMITER $$

CREATE FUNCTION CustomerLevel(p_creditLimit double) RETURNS VARCHAR(10)
    DETERMINISTIC
BEGIN
    DECLARE lvl varchar(10);

    IF p_creditLimit > 50000 THEN
 SET lvl = 'PLATINUM';
    ELSEIF (p_creditLimit <= 50000 AND p_creditLimit >= 10000) THEN
        SET lvl = 'GOLD';
    ELSEIF p_creditLimit < 10000 THEN
        SET lvl = 'SILVER';
    END IF;

 RETURN (lvl);
END

So Can You just little describe about this.

but i need little help according to this example. Thanks.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
TarangP
  • 2,711
  • 5
  • 20
  • 41
  • the answer to your question is provided here: https://stackoverflow.com/questions/2680745/differences-between-procedure-and-function-and-routine – iLikeMySql Sep 20 '17 at 12:40

1 Answers1

0

Tarang , SQL has some build in functions like TRIM, REVERSE, SUM, MIN etc . These functions you can use in select statements . But if you want a function that is not built in ( i.e not part of SQL standard or vendor provided) then you must write your own functions. MySQL Stored functions are user defined functions that are compiled (by engine)and ready to be invoked in the Select statement. Stored procedures are called using "CALL" statement ( which is slightly different). Please refer to some basic SQL tutorial or books which should be make it clear for you. In your example , you must create the function "CustomerLevel" by running the second part of your code example, and then run the SELECT statement .