MySQL Forums
Forum List  »  Stored Procedures

Re: Can't get results from stored function
Posted by: Jay Pipes
Date: February 07, 2006 09:36AM

Currently, you cannot use a variable in the LIMIT statement (there is already a feature request for this functionality). You can, however, use the PREPARE syntax for dynamic SQL.

however, are you sure that you want to use a function for this type of stuff? It would be more efficient to simply execute a standard query:

SELECT MAX(enddate) as maxdate
FROM orderitems
WHERE orderNumber = @orderNum;

If you needed to use this in another query, just use a subquery:

SELECT * FROM orderitems
WHERE orderNumber = @orderNum
AND enddate =
(
SELECT MAX(enddate) as maxdate
FROM orderitems
WHERE orderNumber = @orderNum
);

or if you want to get all order items for ALL orders that were ordered on on each order's last date, use a derived table:

SELECT * FROM orderitems oi
INNER JOIN (
SELECT orderNumber, MAX(enddate) as maxdate
FROM orderitems
GROUP BY orderNumber
) dt
ON oi.orderNumber = dt.orderNumber
AND oi.enddate = dt.enddate;

This last query assumes, of course, that there is a unique key on (orderNumber, enddate). If not, then some things might have to be changed a bit.

Jay Pipes
Community Relations Manager, North America, MySQL Inc.

Got Cluster? http://www.mysql.com/cluster
Personal: http://jpipes.com

Options: ReplyQuote


Subject
Views
Written By
Posted
1747
February 06, 2006 11:12PM
Re: Can't get results from stored function
1329
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.