Solved

Not allowed to return a result set from a function.

Posted on 2014-04-03
3
3,406 Views
Last Modified: 2014-04-04
Hi Experts,

 Can anyone tell me why I keep getting the following  error, when my goalis to return a single value?

error 1415 (0A000): Not allowed to return a result set from a function.

My code
 DELIMITER $$

 CREATE FUNCTION get_author_cost(auth_id SMALLINT(5)) RETURNS DECIMAL(10,2)

 BEGIN

 DECLARE dTotal DECIMAL(10,2);

 SELECT @dTotal:=SUM(price) 
 FROM books
 WHERE author = auth_id;

 RETURN dTotal;

 END $$


 DELIMITER ;

Open in new window

0
Comment
Question by:APD_Toronto
3 Comments
 
LVL 38

Expert Comment

by:Jim P.
ID: 39977953
Try changing dTotal to @dTotal.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 39978001
actually, I would suggest to do the other way round, and not use @, as it's for session-level variables ...
DELIMITER $$

 CREATE FUNCTION get_author_cost(auth_id SMALLINT(5)) RETURNS DECIMAL(10,2)

 BEGIN

 DECLARE dTotal DECIMAL(10,2);

 SELECT SUM(price)  
     INTO dTotal
 FROM books
 WHERE author = auth_id;

 RETURN dTotal;

 END $$
 DELIMITER ;

Open in new window

0
 
LVL 40

Assisted Solution

by:Vadim Rapp
Vadim Rapp earned 250 total points
ID: 39978128
In the function, you can use syntax select column into variable, but not select variable:=column. Even though the result is the same, mysql expects select with into. From documentation:

http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html

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

So, when it encounters select without into, it generates an error.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now