Thursday 30 October 2014

Difference when declaring variables in Mysql / SQL

You may have notice that there is a lot of differences in writing queries in SQL vs. Mysql; One of this difference concerns the definition of variables:

One way to do it in SQL :

DECLARE @ID VARCHAR(15)
SELECT  @ID ='000000001'

SELECT * from [TABLE] (nolock) WHERE  ID = @cID;

(we could use SET instead of SELECT - here, we have used VARCHAR but  any other kind of variable could have been used!!)

The equivalent in Mysql will be the following :

SELECT @ID:= '000000001';

SELECT * from [TABLE] (nolock) WHERE  ID = @cID;