Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get and assign the return value from MySQL stored procedure

I have the next stored procedure:

DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `getUserIdByLogin`(userId VARCHAR(255))
BEGIN
    SELECT id FROM `userdata` WHERE login = userId;
END

I want to declare a new variable @tmp for e.g. and do smth to this:

SET @tmpValue = CALL getUserIdByLogin("someLogin");

But it doesn't work.

If just to call:

CALL getUserIdByLogin("someLogin");

Then I would see results, but I need to declare the results in the variable ( of array type ).

How can I do it?

Thanks!


1 Answers

CREATE DEFINER=`root`@`localhost` PROCEDURE `getUserIdByLogin`(
   userId VARCHAR(255),
   OUT idout int
)
BEGIN
    SELECT id INTO idout FROM `userdata` WHERE login = userId;
END

then

SET @id = 0;
CALL getUserIdByLogin("someLogin",  @id);
SELECT @id;
like image 119
gbn Avatar answered Dec 30 '25 15:12

gbn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!