MySQL - SELECT INTO within a Stored Procedure - mysql

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();

Related

MySQL stored procedure `EXECUTE` command with user defined variable rather than input parameter

I am trying to create a stored procedure in MySQL which is not supposed to be vulnerable to SQL injection. Hence I am using prepared statements inside this. I have a Patient table to which I want to add data using this procedure. This is what my stored procedure looks like.
DROP PROCEDURE IF EXISTS CreatePatient;
DELIMITER ##
CREATE PROCEDURE CreatePatient (IN alias VARCHAR(20))
BEGIN
PREPARE q1 FROM 'insert into Patient values (?)';
set #alias = alias;
EXECUTE q1 USING #alias;
END ##
DELIMITER ;
When I tried to run this without setting a new variable #alias,
EXECUTE q1 USING alias;
I am getting an SQL syntax error. From my understanding, it doesn't seem right to create a variable within the method body just to assign it the input variable to the procedure. What am I missing here?
Mysql has 3 types of variables
User Defined Variables
Local variables
session variables
User defined variables have session scope while local variables have a block scope i.e within BEGIN-END Block.
Because local variables are in scope only during stored program execution, references to them are not permitted in prepared statements created within a stored program. Prepared statement scope is the current session, not the stored program, so the statement could be executed after the program ends, at which point the variables would no longer be in scope. For example, SELECT ... INTO local_var cannot be used as a prepared statement. This restriction also applies to stored procedure and function parameters
See official docs

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.

Best way to call sproc for all the results of query

[SQL Server 2008 Std]
I have a stored procedure that is used to insert new data into a table, which works as expected. However I now need to call this stored procedure multiple times using the results of a query as the parameters.
e.g.
select
name, age, foo, bar
from
sometable
where
wobble = 'true'
exec insertProc name age foo bar
I know I can use a cursor to acheive this, but I keep reading that "cursors are bad"...but I don't know any other way to do this?
One solution is to use cursor. Other is to prepare your result set into temp table before calling the procedure and then supply it to the procedure ( you have to alter the procedure by adding table-value param as input param). Some info in msdn.

Return array in stored function

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?

Stored Procedures last access time?

Is it possible to find out when a Stored Procedure was last accessed?
I tried the following:
SELECT *
FROM sys.dm_db_index_usage_stats
WHERE [database_id] = DB_ID()
AND [object_id] = OBJECT_ID('stored procedure name')
and it returns a blank resultset.
I believe this is possible should the sproc still be in the procedure cache on the server at which point you can query sys.dm_exec_query_stats.
After that you are down to logging and tracing I'm afraid.
Add a log entry as the first line of the stored procedures:
insert into dbo.ProcLog (procname, date) values ('MyProc',getdate())
here is a generic line of code you can place in every procedure, it will include the proper procedure name, without hard coding it.
INSERT INTO YourLog
(RunDate,ProcedureName,...)
VALUES
(GETDATE(),OBJECT_NAME(##PROCID),...)