如何在 t-sql 中声明一个可为空的局部变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27777159/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:07:59  来源:igfitidea点击:

How to declare a nullable local variable in t-sql?

sqlsql-servertsql

提问by ricky

I tried the following statement but failed with compile error. declare @myValue int NULL

我尝试了以下语句,但因编译错误而失败。 declare @myValue int NULL

回答by P?????

If you are trying to make the variable as NOT NULLvariable which will not accept any NULLvalues then I don't think it is possible.

如果您试图将变量设为NOT NULL不接受任何NULL值的变量,那么我认为这是不可能的。

MSDNsays

MSDN

After declaration, all variables are initialized as NULL, unless a value is provided as part of the declaration

声明后,所有变量都初始化为NULL,除非在声明中提供了一个值

so you don't have to mention it explicitly

所以你不必提及它 explicitly

回答by Ganesh_Devlekar

Your syntax is wrong. By default all variables are nullable

你的语法是错误的。默认情况下,所有变量都可以为空

 declare @myValue int=NULL

OUTPUT

输出

 select @myValue
 NULL