创建mysql函数

时间:2014-11-24 19:30:27

标签: mysql

我想创建一个具有以下查询的函数,但会发生错误

DELIMITER $$
CREATE FUNCTION getOnlineUserTraffic (x1 VARCHAR(32)) 
RETURNS FLOAT
DETERMINISTIC
BEGIN 
  DECLARE dist FLOAT;
  SET dist = SELECT UsedCreditTraffic FROM uonlineuser WHERE UserName=x1
  RETURN dist;
END$$
DELIMITER ;

// userName = char,长度= 32

// UsedCreditTraffic = float

1 个答案:

答案 0 :(得分:1)

尝试:

DELIMITER $$

DROP FUNCTION IF EXISTS getOnlineUserTraffic$$

CREATE FUNCTION getOnlineUserTraffic(x1 VARCHAR(32)) 
RETURNS FLOAT
DETERMINISTIC
BEGIN 
  DECLARE dist FLOAT;
  SET dist = (SELECT UsedCreditTraffic FROM uonlineuser WHERE UserName=x1);
  RETURN dist;
END$$

DELIMITER ;

SQL Fiddle demo