DELIMITER //
CREATE FUNCTION fnc_credit_custstatus
RETURNS VARCHAR(6) DETERMINISTIC
BEGIN
DECLARE custstatus VARCHAR(6);
IF CustCredit>=1000 THEN SET custstatus='VIP';
ELSEIF CustCredit<1000 THEN SET custstatus='NONVIP';
END IF;
RETURN custstatus;
END //
DELIMITER ;
I think I created FUNCTION has succeeded,but when I SELECT the function is Error Code: 1054 Unknown column 'fnc_credit_custstatus' in 'field list'
SELECT*, fnc_credit_custstatus AS custstatus
FROM customer_salon
You do have to pass a parameter..and this doesn't syntax because you don't define one.
drop function if exists f;
DELIMITER //
CREATE FUNCTION f(custcredit int)
RETURNS VARCHAR(6) DETERMINISTIC
BEGIN
DECLARE custstatus VARCHAR(6);
IF CustCredit>=1000 THEN SET custstatus='VIP';
ELSEIF CustCredit<1000 THEN SET custstatus='NONVIP';
END IF;
RETURN custstatus;
END //
DELIMITER ;
set @cr = 2000;
select @cr,f(@cr);
+------+--------+
| @cr | f(@cr) |
+------+--------+
| 2000 | VIP |
+------+--------+
1 row in set (0.001 sec)
or in your case
SELECT *, f(custcredit) AS custstatus FROM customer_salon