Returning a value from a UDF. (MySQL)
Attempting to create the following function keeps giving me 'Not allowed to return a result set from a function' ... Google has shown me no love. Nor has reading the MySQL docs/examples.
DELIMITER $$
CREATE
FUNCTION `sparks`.`ChildCheck`(ParentType VARCHAR(2), ParentID VARCHAR(4)) RETURNS INTEGER
BEGIN
DECLARE ParentString VARCHAR(8);
DECLARE Kids INTEGER;
SET ParentString=CONCAT(ParentType, ':',ParentID);
SET Kids = 0;
SELECT Kids=Kids+1 AS KidCount
FROM (SELECT parent FROM mod_CMS WHERE dept_id=1 AND parent=ParentString
UNION
SELECT parent FROM mod_External_Links WHERE dept_id=1 AND parent=ParentString
UNION
SELECT parent FROM dept_mods WHERE dept_id=1 AND parent=ParentString) t1;
RETURN (KidCount);
END$$
DELIMITER ;
That doesn't work.
The following 开发者_如何学JAVADOES work.
DELIMITER $$
DROP FUNCTION IF EXISTS `next_april1`$$
CREATE FUNCTION `next_april1`(inDate DATETIME) RETURNS DATE
DETERMINISTIC
BEGIN
DECLARE outDate DATE;
SET outDate = (SELECT CASE
WHEN MONTH(inDate) < 4 THEN CONCAT(YEAR(inDate), '-04-01')
ELSE CONCAT(YEAR(inDate) + 1, '-04-01')
END);
RETURN (outDate);
END$$
DELIMITER;
The syntax for the working one is:
select next_april1(NOW());
and it returns
'2012-04-01'
I'm looking for the first one to work like:
select KidCount('C','3')
Output: 4
I must be missing something between the two functions.
Per the documentation for Create Procedure:
Statements that return a result set can be used within a stored procedure but not within a stored function. This prohibition includes SELECT statements that do not have an INTO var_list clause and other statements such as SHOW, EXPLAIN, and CHECK TABLE.
精彩评论