2

I am trying to declare a variable in SQL. I Have tried both

declare @mean INT;
set @mean = .5;

and

declare @mean INT
set @mean = .5

I keep getting this error:

An unexpected token "" was found following "". Expected tokens may include: "declare @mean INT"

Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54

2 Answers2

2

If you want to declare your variable as a loosely-typed user-defined variables are defined using '@', you can simply go:

SET @mean := .5 

If not, then perhaps you want to define a variable in a stored procedure, then you can perhaps define as follows:

DECLARE  mean INT;
SET mean = .5;      // not sure you mean INT here though?

You may find this SO link useful for describing the differences in my sql variables. Also check out the mysql manual(user variables) / (local variables).

I think some of the confusion here may stem from the differing syntax that T-SQL uses from Mysql, but read the above link and use whatever method suits you best.

Community
  • 1
  • 1
freya19
  • 43
  • 6
1

If this is in a stored procedure, you don't need the @ for a local variable. If this is just a session variable you are trying to create, you don't need a DECLARE; just SET @mean := 5;

I'm not positive, but I think DECLARE may not even be valid outside of stored procedures.

Uueerdo
  • 15,723
  • 1
  • 16
  • 21