MySQL Forums
Forum List  »  Stored Procedures

Can't get results from stored function
Posted by: Luis Rocha
Date: February 06, 2006 11:12PM

Hello, I am trying to write a function that will return the oldest date from a list of items. These items are in a table ordereditems, and have a relationship to the orders in table orders by the orderNumber. The code for the function is:
--------------------------------------------------------------------------------------------------
CREATE FUNCTION OLDEST_EXPIRE(orderNum INT) RETURNS DATE
BEGIN
SET @tempDate = FROM_UNIXTIME(0);
SET @orderNum = orderNum;
SET @oldestDate = NOW();
SET @rowIndex = 0;
SET @totalRows = (SELECT COUNT(*) FROM ordereditems WHERE ordereditems.orderNumber = @orderNum);
checkDates: LOOP
SELECT ordereditems.enddate INTO @tempDate FROM ordereditems WHERE ordereditems.orderNumber = @orderNum LIMIT @rowIndex, 1;
IF @tempDate < @oldestDate THEN
SET @oldestDate = @tempDate;
END IF;
SET @rowIndex = @rowIndex + 1;
IF @rowIndex = @totalRows THEN
LEAVE checkDates;
END IF;
END LOOP checkDates;
RETURN DATE_FORMAT(@oldestDate,GET_FORMAT(DATE, 'ISO'));
END;
--------------------------------------------------------------------------------------------------
When I try to run that code through the client or phpmyadmin, I get the following error

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@rowIndex,1;
IF tempDate < oldestDate THEN
SET oldestDate = tempDate;
END' at line 10

and I can't figure out why it's ocurring. I tried changing the name of the variable referrenced there, taking out the @, but nothing has worked so far.

Additionally, when I make say LIMIT 1 instead of LIMIT @rowIndex, 1 then the function returns 0 (or rather the date that corresponds to timestamp 0). The oldestDate variable was never changed. I'm assuming this is because it's only checking the first item every time through the loop, but even the first item for the test order has a date present. Any help would be appreciated.

Luis F. Rocha

Options: ReplyQuote


Subject
Views
Written By
Posted
Can't get results from stored function
1755
February 06, 2006 11:12PM
1331
February 07, 2006 09:36AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.