MrB
MrB

Reputation: 1594

MySql set multiple variables from one select

I'm starting to pull my hair out here is something like this even possible?

DECLARE var1 int;
DECLARE var2 int;
DECLARE var3 int;
SELECT var1:=id, var2:=foo, var3:=bar from page WHERE name="bob";
CALL someAwesomeSP (var1 , var2 , var3 );

The above doesn't work but i am trying to figure out how I would accomplish this. My ultimate goal here is to call a select and than call stored proc with data from select.
Thanks

Upvotes: 30

Views: 63762

Answers (2)

young-ceo
young-ceo

Reputation: 5384

For MySQL, please take a look this example code:

-- Init variables
SET @var1 = 0;
SET @var2 = 0;
SET @var3 = 0;
SELECT VALUE1, VALUE2, VALUE3 INTO @var1, @var2, @var3 FROM COOL_TABLE WHERE VALUE_ID = 12345;

-- Then you can use declared variables
SELECT * FROM ANOTHER_TABLE WHERE VALUE1 = @var1 AND VALUE2 = @var2

Upvotes: 33

MrB
MrB

Reputation: 1594

This worked for me.

        DECLARE var1 int;
        DECLARE var2 int;
        DECLARE var3 int;
        SELECT id, foo, bar INTO var1, var2, var3 from page WHERE name="bob";
        CALL someAwesomeSP (var1 , var2 , var3 );

Thanks to Zec. The first link helped me understand the correct syntax or at least what is working for me.

Upvotes: 30

Related Questions