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
Related
I was trying to get data into the Output parameters of Stored Procedure in mysql but I am not getting it back.
HERE IS THE QUERY
Creation
CREATE DEFINER=`root`#`localhost` PROCEDURE `get_initial_data`(
out usersData varchar(500),
out employeesData varchar(500)
)
BEGIN
SELECT * into usersData FROM users;
SELECT * into employeesData FROM employees;
END
Calling
Call get_initial_data(#users, #employees)
select #users
select #employees
I tried this and I am able to create the Store Procedure but not able to call, its giving me this Error...
Error Code: 1172. Result consisted of more than one row
Can you help me in this, am I passing the Output parameters correctly and also the Data type of that?
Please let me know your response on this....
An output parameter can contain only a single value. You are trying to return result sets via the output variable. This is not how output parameters work.
You read the result sets coming from the procedure; no need to use output variables.
CREATE PROCEDURE get_initial_data()
BEGIN
SELECT * FROM users;
SELECT * FROM employees;
END
Output parameters are useful in a situation where you have a procedure calling another procedure and use the output of the called procedure. Even then, you can only use single values with output parameters.
I have a Stored Procedure wrote in SQL Server that I need to convert to MySQL, which has this code inside.
SQL server code
declare #resultatSP table (...);
...
insert into #resultatSP execute other_procedure(...);
...
My problem is that in SQL Server, the "result" of the last select done in the SP other_procedure is stored in #resultatSP. (I don't know if it is conventional but it works), but I cannot reproduce it in MySQL.
I tried things like this:
MySQL code
drop temporary table if exists resultatSP;
create temporary table resultatSP (...);
...
resultatSP = CALL other_procedure(...);
OR
CALL other_procedure INSERT INTO resultatSP;
But nothing works.
I read that in MySQL, call stored procedure does not return result and we must use OUT/INOUT parameters. But I cannot change the other_procedure(...).
What is the most confusing is that this other_procedure has been convert in MySQL and it is used in java with some Javax.persistance annotations as #NamedNativeQuery or #SqlResultSetMapping and these annoations succeed to get the "result" of the last select of the stored procedure other_procedure(...) converted in MySQL. I don't know how these annotations make it.
I've got a procedure that uses a loop with a SELECT statement, but the statement is actually just to set a variable. That means there's a lot of stuff being displayed that I don't need to see, and it's flooding my terminal.
Here's an example of what I mean, though this isn't actually what I'm running (because that's company information):
DROP PROCEDURE IF EXISTS test;
DELIMITER #
CREATE PROCEDURE test()
BEGIN
SET #key:=1;
testloop: REPEAT
SELECT
#dummyString := stringField
FROM
aTable;
SET #dummyStringAll :=CONCAT(#dummyStringAll,$dummyString);
SET #key := #key + 1;
UNTIL #key>10
END REPEAT testloop;
END #
DELIMITER ;
Is it possible to run SELECT (whether inside a procedure or not) and not show the results from a SELECT query? Maybe not the most important thing in the world, but it would be helpful.
Stored procedures will return a query resultset if it isn't stored in a variable.
How does it know that you are storing the result in a variable?
Not be using variables in the query but by using the SELECT value INTO <variable> syntax in the query. see: 13.2.9.1 SELECT ... INTO Syntax
From the FAQ:
1) Can MySQL 5.6 stored routines return result sets?
Stored procedures can, but stored functions cannot. If you perform an ordinary SELECT inside a stored procedure, the result set is returned directly to the client.
So, using the 'SELECT ... INTO ...' syntax will prevent the procedure returning the resultset from a query.
My first stored procedure returns 3 values which I will use in another stored procedure. I tried like this:
CREATE DEFINER=`db`#`%` PROCEDURE `SP_Expense`(
flightInstanceId INT
)
begin
SELECT F.BaseFare, CALL SP_Orderdetail(F.fareId)
FROM fare;
end$$
but I'm getting a syntax error.
Is it possible in functions? Which is return table ?
You cant call a stored procedure from with in a select statement. What you are doing is trying to call a function. Create Fucntion. If I understand your question SP_Orderdetail(F.fareId) returns a table? I wonder if you could turn SP_Orderdetail(F.fareId) into a view instead?
This might help as well: Create Stored procedure and Create function syntax
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?