简体   繁体   中英

Declare @ variable in SQL returning an error

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"

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM