无法在SQL中为本地变量分配默认值 [英] Cannot assign a default value to a local variable in SQL
问题描述
我正在尝试声明局部变量,例如:
I am trying to declare local variable like:
DECLARE @thresholdDate DATETIME = '2014-11-30'
我得到了错误:
无法将默认值分配给局部变量。
Cannot assign a default value to a local variable.
根据文档:
DECLARE @find varchar(30);
/* Also allowed:
DECLARE @find varchar(30) = 'Man%';
*/
我在做什么错?
推荐答案
在SQL Server 2008之前,不允许将默认值(或初始值)分配给局部变量。否则将遇到此错误消息。
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.
解决方案1:(使用 SET
)
DECLARE @thresholdDate DATETIME
set @thresholdDate = '2014-11-30'
有关该错误的更多详细信息: http://www.sql-server-helper.com/error-messages/msg-139.aspx
For more details about the error : http://www.sql-server-helper.com/error-messages/msg-139.aspx
解决方案2:(升级)
避免此错误的另一种方法,这有点远获取的解决方案是升级到SQL Server2008。SQLServer 2008现在允许在DECLARE语句中为变量分配值。
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中为本地变量分配默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!