Site Loading

MySql Revelations

I am used to MS Sql when writing web based applications but recently I have started working for a company that uses MySql.  This has been fairly similar in most cases but there have been some differences that I have battled to find answers to online so I thought I would put them here and hopefully they will help someone else (like me) who is newish to MySql.

 

The first issue was the declaration of variables.  It seems that MySql doesn’t require the declaration of variables (Although I think they then become global which is probably a bad thing if you are reusing the variables,  I will look into this).

So in Ms Sql, DECLARE @Var int

In MySql you dont need the declare, you can just set it Directly: SET @Var = ‘somevalue’;

 

Once I was over the first hurdle I came up against the setting the variable in a select statement.  I was trying the following (which didnt set the variable)

SET @Var = 1;

SELECT @Var = 5;

SELECT @Var;

The result was 1.  The solution I found was the following operator: :=

So then the follow worked for me:

SET @Var = 1;

SELECT @Var := 5;

SELECT @Var;

 

I hope that this helps.  If you have more revelations for a MySql starter then please let me know and I will post them here to help all the new guys.

kick it on DotNetKicks.com

Close