mysql函数无法正常工作

时间:2016-05-29 09:06:18

标签: mysql

我的数据库名称是dfeb,我有一个名为invdt的表和名为batch_numberid的列。我想得到batch_number所在的id = 59,使用函数。但错误显示为function does not exist

 DELIMITER $$
CREATE
FUNCTION `dfeb`.`get_quantity`(PID)
RETURNS VARCHAR
BEGIN
DECLARE NAME_FOUND VARCHAR DEFAULT "";
SELECT `batch_number` INTO NAME_FOUND FROM `invdt` WHERE ID = PID;
RETURN NAME_FOUND
END$$

 DELIMITER ;

SELECT `get_quantity`(59) FROM `invdt` 

1 个答案:

答案 0 :(得分:0)

尝试:

DELIMITER $$

-- CREATE FUNCTION `dfeb`.`get_quantity`(PID)
CREATE FUNCTION `dfeb`.`get_quantity`(PID INT)
-- RETURNS VARCHAR
RETURNS VARCHAR(200)
BEGIN
  -- DECLARE NAME_FOUND VARCHAR DEFAULT "";
  DECLARE NAME_FOUND VARCHAR(200) DEFAULT "";
  SELECT `batch_number` INTO NAME_FOUND FROM `invdt` WHERE ID = PID;
  -- RETURN NAME_FOUND
  RETURN NAME_FOUND;
END$$

DELIMITER ;

-- SELECT `get_quantity`(59) FROM `invdt`
SELECT `dfeb`.`get_quantity`(59) FROM `invdt`;

<强>更新

CREATE FUNCTION `dfeb`.`get_quantity`(PID INT)
.
.
.

-- SELECT `get_quantity`(59) FROM `invdt`
SELECT `dfeb`.`get_quantity`(59) FROM `invdt`;

或者

USE `dfeb`;

CREATE FUNCTION `get_quantity`(PID INT)
.
.
.

SELECT `get_quantity`(59) FROM `invdt`;