Click to See Complete Forum and Search --> : [RESOLVED] MySql declare variable


dannystommen
May 14th, 2009, 11:27 AM
I'm trying to declare a variable:

BEGIN
DECLARE @test INT;
SET @test = 5;

SELECT @test;
END;


When I try to execute the code using mysql query browser, the declare line fails, but the set line succeeds. Finally when I run the select line '5' is displayed.

But even when I change SET @test = 5; to SET @test = 'test'; this line also succeeds and the select line shows 'test'.

What am I doing wrong. I want to execute this code from within my webapplication, but it immediatly fails on the first line, and everything after it is not executed.

kevin.horgan
May 29th, 2009, 09:19 AM
Hi there,

I do not use My SQL normally but from what you describe it sounds like MySQL does not need the DECLARE command in this case. That is your SET command seems to be doing the DECLARE and the ASSIGNMENT.

So I would either 1) remove the DECLARE and just use the SET as it is, or 2) change the SET to be a SELECT like this...

DECLARE @test INT;
SELECT @test = 5;

SELECT @test;

If MySQL does support the DECLARE command then the SELECT will only do the assignment which I think is what you are trying to do with the SET command.

I hope that helps.

Cheers,

Kevin

dannystommen
May 29th, 2009, 09:22 AM
Solved the problem already but forgot to mark the thread as resolved.

Anyway, the answer is dat MySql does not need the declaration line.

SET @test = 5;

--OR

SET @test = 'value';