Beginners' guide to stored procedures with MySQL? - mysql

I've Googled but come up with nothing that I can get my head around.
Are the performance gains from using stored procedures significant?
Would I still want to use prepared statements in conjunction with stored procs or is it generally a one or the other thing?
Can I create stored procs through PHPMyAdmin and manage them from there as well?
What would a stored procedure look like for something simple like this-
SELECT * FROM table a
INNER JOIN otherTable b
ON a.join_id=b.join_id
WHERE someVar = :boundParam
and how would the PHP work (PDO) to call it and bind its parameter?

Consider this a gentle introduction to stored procedures in MySQL: http://www.mysqltutorial.org/mysql-stored-procedure-tutorial.aspx
You sure can create/manage stored procedures in phpMyAdmin.

I have created one procedure but the time of call this procedure what parameter I can pass to get the output
CREATE DEFINER=`root`#`localhost` PROCEDURE `A`(
In user_id bigint,
Out address varchar(250)
)
BEGIN
select Address into address
from UserDetail_table
where User_ID = user_id;
END

Related

Storing MySQL Stored Procedure Result Into Temporary Table

In SQL Server, I used to create a table variable to store results from a certain stored procedures. This is how I usually do with table variable.
DECLARE #My_Table_Variable TABLE(col_1 FLOAT, col_2 FLOAT)
INSERT INTO #My_Table_Variable(col_1, col_2) EXEC [My_Procedure]'param_1','param_2'
Now, while using MySQL, I recognized that table variable doesn't exist. I've seen some questions related to this, for instance, this one says it's not possible to SELECT something FROM a procedure.
How about temporary table? Can we CALL a procedure, and then put the result into a temporary table? With the syntax something like this:
CALL my_procedure('my_first_parameter','my_second_parameter') INTO my_temporary_table;
which allows me to query from my_temporary_table. Is this possible to be performed?

How to pass ID list to stored procedure using Hibernate in Grails

I am trying to pass an ID list as a parameter to MySQL stored procedure in Grails. I found a workaround that does work but the problem is that it is too slow since it uses FIND_IN_SET MySQL function that iterates through an id list passed in as a string ("1,2,3,4").
Procedure call:
Sql sql = new Sql(dataSource)
def rawQueryResult = sql.rows("call testProcedure(?)", ["1,2,3,4"])
Stored procedure:
CREATE DEFINER=`root`#`localhost` PROCEDURE `testProcedure`(
IN idList TEXT,
)
BEGIN
SELECT tt.id
FROM test_table tt
WHERE FIND_IN_SET(tt.id, idList);
END
This does work however when I have a large ID set, the procedure execution lasts for a long time. Is there a better way to pass ID list to procedure.
This does work however when I have a large ID set, the procedure
execution lasts for a long time. Is there a better way to pass ID list
to procedure.
No. There aren't other ways to pass the ID list into a stored procedure that would be better in terms of how long the execution of the stored procedure takes.

MySQL stored procedures out variable as array

I have MySQL procedure where I want to get a result of query:
SELECT id FROM mbus_clients WHERE second_name like surnamePart AS
So it should be an array. The decision I've found in the internet is to use temporary table.
But how can I return a table and read with PHP? Is it ok?
Simply call the procedure:
CALL procedurename();
If the procedure performs a SELECT, the result set of the procedure call will be the same as if you'd performed the query itself. You can then fetch the rows using PHP the same way as if you'd performed a SELECT.

save stored procedure output into a table

I have execute only access to a stored procedure.
This SP seems to select some data from multiple tables, and returns one row. I need to store two columns of the output of this SP into a table.
Is there any way to do this within MySQL?
If it returns a row, this is a stored function and not a stored procedure. You can use something like the following to insert into your table:
INSERT INTO tablename SELECT (SELECT col1, col2 FROM (SELECT somefunction()))
Otherwise, it will be a stored procedure and you should do something like this, assuming that #var1 and #var2 are output parameters:
CALL someprocedure(#var1, #var2, #var3)
INSERT INTO tablename SELECT(#var1, #var2)
See the documentation about Create Procedure and Create Function for more information about functions versus procedures.
MySQL has an extension to stored procedures that allows the procedure to return one or more result sets to the client, as if the client had issued a SELECT query... but those results are ephemeral. They don't persist and they can't be stored in variables or otherwise accessed after the procedure finishes -- they can only be "fetched" the one time.
There is a way to make them accessible without breaking the way the procedure already works, as I discussed here, but you can't do it without a change to the procedure:
How to use Table output from stored MYSQL Procedure
The idea is for the procedure to write its output in a temporary table, and then return it to the caller by calling SELECT against the temporary table -- but to leave the temporary table behind so that the caller can access it directly if desired.
That's not exactly the same as what you're asking though, which is why I didn't mark this question as a duplicate, since you, unlike the other poster, do not appear to have administrative control of the procedure... but unless you can make the case for a change like this, there's not another way within MySQL to access those returned values, since they only exist in the result-set that's returned.
Of course, procedures do have optional OUT parameters, where you can hand variables to the procedure as part of arguments you use to call it, and it can set those variables, so that they'll have the values you need when the procedure is done, but that only works when the return values are scalars and would require a change to the procedure's interface, since procs in MySQL do not have "optional" arguments... if the procedure were changed to permit this, it would require an increased number of arguments to be provided every time it was called, and if other components are calling it, that could easily break other things.

What is the equivalent of Oracle’s REF CURSOR in MySQL?

I am trying to make a procdure in mysql that returns me an array with the result, I used to do with the oracle ref cursor, but in mysql do not know how to proceed,
I have to pass parameters too...
Anyone know how I can do, or have an example to show me? Thank you very much...
MySQL doesn't have a refcursor like Oracle. If you are planning to write a stored procedure that returns multiple rows/result set in MySQL just do
DROP procedure IF EXISTS `sample`;
DELIMITER $$
CREATE PROCEDURE `sample`(p_rank IN INT)
BEGIN
select * from MyTable where id=p_rank;
END$$
DELIMITER ;
and call sample();. It will return a result set which you can use.
There is no analog of REF CURSOR in MySQL. Stored procedures and functions allow to pass and return only scalar datata types, see the reference here - CREATE PROCEDURE and CREATE FUNCTION Syntax.
MySQL cannot operate with arrays. A workaround is to use a table (or TEMPORARY TABLE).
Also - take advantage of visual object editors and stored procedure debugger in dbForge Studio for MySQL.