无法为 SQL 中的局部变量分配默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27060586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot assign a default value to a local variable in SQL
提问by Vladimirs
I am trying to declare local variable like:
我正在尝试声明局部变量,如:
DECLARE @thresholdDate DATETIME = '2014-11-30'
And I am getting error:
我收到错误:
Cannot assign a default value to a local variable.
无法为局部变量分配默认值。
As per documentation:
根据文档:
DECLARE @find varchar(30);
/* Also allowed:
DECLARE @find varchar(30) = 'Man%';
*/
What I am doing wrong?
我做错了什么?
回答by Veera
Prior to SQL Server 2008, assigning a default value (or initial value) to a local variable is not allowed; otherwise this error message will be encountered.
在 SQL Server 2008 之前,不允许为局部变量分配默认值(或初始值);否则会遇到这个错误信息。
Solution 1:(Use SET
)
解决方案1:(使用SET
)
DECLARE @thresholdDate DATETIME
set @thresholdDate = '2014-11-30'
For more details about the error : http://www.sql-server-helper.com/error-messages/msg-139.aspx
有关错误的更多详细信息:http: //www.sql-server-helper.com/error-messages/msg-139.aspx
Solution 2:(Upgrade)
解决方案2: (升级)
Another way of avoiding this error, which is a little bit a far-fetched solution, is to upgrade to SQL Server 2008. SQL Server 2008 now allows the assigning of a value to a variable in the DECLARE statement.
避免此错误的另一种方法(有点牵强)是升级到 SQL Server 2008。SQL Server 2008 现在允许为 DECLARE 语句中的变量赋值。
回答by CODeeerrrrrrrr
You will hit this error
你会遇到这个错误
Cannot assign a default value to a local variable
无法为局部变量分配默认值
in case you are running SQL Server 2005 or earlier, and you try to declare a variable and assign it a value in one statement.
如果您运行的是 SQL Server 2005 或更早版本,并且您尝试在一个语句中声明一个变量并为其赋值。
Something like -
就像是 -
DECLARE @Var Varchar(15) = 'Test'
Message 139, Level 15, State 1, Line 0
Cannot assign a default value to a local variable
消息 139,级别 15,状态 1,第 0 行
无法将默认值分配给局部变量
Variable declaration has been enhanced in SQL Server 2008 and newer versions, and I realized this today when I was deploying a code for multiple systems.
变量声明在 SQL Server 2008 和更新版本中得到了增强,我今天在为多个系统部署代码时意识到了这一点。
Apparently we also have the flexibility to just use one Declare statement to declare multiple variables -
显然我们也可以灵活地只使用一个 Declare 语句来声明多个变量——
DECLARE @Var Varchar(15) = 'Test',
@Char Varchar(10) = 'Test2',
@Char2 Varchar(10) = 'Test3'
In SQL Server 2005 or earlier, you will need to declare the variable and then assign it a value using Set statement.
在 SQL Server 2005 或更早版本中,您需要声明变量,然后使用 Set 语句为其赋值。
Example -
例子 -
DECLARE @Var Varchar(15)
SET @Var = 'Test'
回答by Saravana Kumar
The error
错误
Cannot assign a default value to a local variable
无法为局部变量分配默认值
occurs if you are using SQL Server 2005while assigning default value
to local variable
.
如果您使用的是发生SQL Server 2005中,而分配default value
到local variable
。
For SQL Server 2005use this code:
对于SQL Server 2005,请使用以下代码:
DECLARE @thresholdDate AS DATETIME
SET @thresholdDate = '2014-11-30'
SELECT @thresholdDate
For SQL Server 2008 and neweryou can use this instead:
对于SQL Server 2008 和更新版本,您可以使用它:
DECLARE @thresholdDate DATETIME = '2014-11-30'
SELECT @thresholdDate