I am trying to declare a variable in SQL. I Have tried both
我试图在SQL中声明一个变量。我试过了两个
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"
在“”之后发现了一个意外的标记“”。预期的代币可能包括:“声明@mean INT”
2 个解决方案
#1
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).
您可能会发现此SO链接对于描述我的sql变量的差异很有用。另请查看mysql手册(用户变量)/(局部变量)。
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.
我认为这里的一些混淆可能源于T-SQL在Mysql中使用的不同语法,但请阅读上述链接并使用适合您的最佳方法。
#2
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;
如果这是在存储过程中,则不需要@作为局部变量。如果这只是您尝试创建的会话变量,则不需要DECLARE;只是SET @mean:= 5;
I'm not positive, but I think DECLARE may not even be valid outside of stored procedures.
我不是肯定的,但我认为DECLARE甚至可能在存储过程之外无效。
#1
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).
您可能会发现此SO链接对于描述我的sql变量的差异很有用。另请查看mysql手册(用户变量)/(局部变量)。
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.
我认为这里的一些混淆可能源于T-SQL在Mysql中使用的不同语法,但请阅读上述链接并使用适合您的最佳方法。
#2
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;
如果这是在存储过程中,则不需要@作为局部变量。如果这只是您尝试创建的会话变量,则不需要DECLARE;只是SET @mean:= 5;
I'm not positive, but I think DECLARE may not even be valid outside of stored procedures.
我不是肯定的,但我认为DECLARE甚至可能在存储过程之外无效。