Return array in stored function - mysql

I am trying to use the result of a stored function in a WHERE statement in MySQL (5.x), but it fails because in the function I am selecting values from a table into an INT variable and then returning them/it, which obviously doesn't work if the SELECT returns more than 1 row. I've tried returning a TABLE (as I understood TABLE means array in MySQL) but that didn't work either.
Is there any way that I could do something like:
SELECT ID FROM myTable WHERE ID IN my_function(params);
Thank you.

This cannot be done...
First, you cannot use a stored function to return multiple results - you would need to use a stored procedure.
The MySQL docs state:
Statements that return a result set can be used within a stored procedure but not within a stored function.
Second, you cannot use a stored procedure in a query - see this SO question.
Have you considered using 'HAVING ...' at the end of your query?

Related

MySQL - SELECT INTO within a Stored Procedure

We have a lot of stored procedures which have an OUT parameter which we are assigning a value to by using SELECT LAST_INSERTID() INTO p_AutoNumber.
After this, we need to perform another INSERT which also uses the value of this variable - however it doesn't appear to run correctly.
Does performing a SELECT INTO on an OUT parameter return from the procedure immedately? I am unable to find any information on this in the MySQL docs for SELECT INTO.
You can use the out-parameter within the procedure. Just check the syntax (and the function name):
Use syntax:
SELECT LAST_INSERT_ID() INTO p_AutoNumber;
Alternatively you can use syntax:
SET p_AutoNumber = LAST_INSERTID();

Throw error for multiple results in PostgreSQL SQL function

Consider the following example function:
CREATE FUNCTION test()
RETURNS INTEGER
LANGUAGE SQL
AS $$ SELECT * FROM UNNEST(ARRAY[1,2,3,4,5]); $$
;
When we execute it like so:
SELECT test();
the result we get back is 1. In other words, the function just returned the first element of the result set.
I have a convenience function that retrieves an ID based on data in the rest of the row. It lets callers query on data that is often unique to a particular row but is not strictly unique. Primarily, it will be used to populate some mostly static data. Allowing the call to use the row data instead of hard coding the ID makes it more readable and easier to maintain when new data does come along. This depends on the caller having some familiarity with the data, but that's a reasonable assumption to make for the usage at hand.
The problem is if the user makes a mistake and gives arguments that are insufficient to filter the query's results to one row, I want my function to error out instead of returning the first result. How can I accomplish this? Do I have any options other than to switch my language to PL/PGSQL and check it manually?
(Using PostgreSQL 9.3).
You could put the entire query as a subquery in the SELECT list. When attempting to return more than one row, this will fail with this error:
ERROR: more than one row returned by a subquery used as an expression
Based on your example, this one would fail:
CREATE FUNCTION test()
RETURNS INTEGER
LANGUAGE SQL
AS $$ SELECT (SELECT * FROM UNNEST(ARRAY[1,2,3,4,5])); $$
and this one wouldn't fail:
CREATE FUNCTION test()
RETURNS INTEGER
LANGUAGE SQL
AS $$ SELECT (SELECT * FROM UNNEST(ARRAY[1])); $$

MySQL: Get result from MySQL procedure call

Situation: Having a SQL procedure which "returns" result via "SELECT x" statements. For some reasons it is not allowed to change it to a function or changing that procedure in any way. How can I obtain the result like:
set #result = 0;
#result = call SomeProcedure(#p1, #p2);
But since it is a procedure not a function above code won't compile/work. How can I achieve that in MySQL. In C++ it works but in MySQL I found no way ...
It is not possible.
Result sets returned from select ... will always be returned to the caller of the first procedure, even if you make several levels of sub calls.
Functions return a value (but not a result set) that you can use inside other procedures or functions.
Your only option is to either set session variables or to store the result in a temporary table that the calling procedure knows about.

Mysql function to return row count from a procedure call [duplicate]

This question already has answers here:
How can I get the number of rows 'returned' from a result set of a stored procedure
(2 answers)
Closed 7 years ago.
I am trying to write a function to return the number of rows a call to a stored procedure would return. I'm trying to minimise repetition of the code (for reduced code maintenance/debugging- the procedure select is long).
The stored procedure just read-only selects rows matching certain criteria (vague I know but details should not be material to the question).
I could just copy the procedure into a function and change the select to count() but as it is long with multiple joins I was hoping to write a function that could call the procedure and return the row count. The goal is not for optimised running but for efficient code maintenance, boiler plate reduction.
I have tried this as a test:
DELIMITER //
CREATE PROCEDURE IF NOT EXISTS proc_select1()
BEGIN
SELECT 1;
END //
CREATE FUNCTION IF NOT EXISTS select1_count() RETURNS INT UNSIGNED
BEGIN
CALL proc_select1();
RETURN FOUND_ROWS();
END //
DELIMITER ;
However when I SELECT select1_count(); - which I am hoping will return 1 - I get the "cannot return a result set from a function" error.
I tried assigning FOUND_ROWS to a variable, clearing the result set then returning the variable value but can't get it to work.
Does anyone know a work around or do I really need to copy-paste the procedure and convert to a SELECT COUNT and function?
I'm using MySQL 5.5.16 (can upgrade if necessary), Windows 7 (nobody seems to want to upgrade :) with HeidiSQLv7.0.0.4053 (if relevant)
As always, any help much appreciated.
first use distinct to get distinct values then use count on that..like
select count(distinct column_name) from table_name
cannot return a result set from a function
This error happens when a SELECT query is done in a procedure without storing the output values.
Other thing : don't forget to use SQL_CALC_FOUND_ROWS to indicates your DBMS to store the number of found rows.
I tried to make it work without any temporary variable (Maybe there is exists a mysql keyword to not "return" the set of the SELECT query), but no success. Here a piece of code which works using temporary var.
CREATE PROCEDURE proc_select1()
BEGIN
DECLARE temp INT;
SELECT SQL_CALC_FOUND_ROWS 1 INTO temp;
END //
CREATE FUNCTION select1_count() RETURNS INT UNSIGNED
BEGIN
CALL proc_select1();
RETURN FOUND_ROWS();
END //
The result is 1 as expected :-) cf : SQLFiddle
It appears this is not possible. The select statement is required to SELECT INTO something to avoid the cannot return a result set from a function error during the function call. If this is not possible or required in the SELECT statement used in the procedure then the function will not run without error.
Using CLEAR QUERY CACHE or FLUSH QUERY CACHE after the procedure call did not help (and is probably a bad idea / bad coding anyway).

How to fetch result of stored procedure in iReport?

I am using Ubuntu 12.04,iReport-4.7,MySQL,mysql-jdbc driver
I write stored procedure in MySQL
DELIMITER //
CREATE PROCEDURE first()
BEGIN
select * from person where id in (11,22,33);
END //
DELIMITER;
which return id & name as fields
& calling it from iReport
select id+1,name from (call first)
gives me syntax error,
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: 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 'call first)' at line 1
but when I run call first in query executor,no error
How should I get required field for further calculations ?
Does this is possible in MS-SQL ?
It is not possible.
You might consider to use temp table.
Please check this SO Post: MySql: Can a stored procedure/function return a table?
Regards