Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 704
ricky Avatar asked Jan 05 '15 10:01

ricky


People also ask

How do you DECLARE a nullable variable in SQL?

The rule for assigning NULL values to variables or table columns is simple: Use keyword "NULL" directly as normal values. Specificly, "NULL" can be used in SET statements to assign NULL values to variables.

Is NULL in T SQL?

If the value of expression is NULL, IS NULL returns TRUE; otherwise, it returns FALSE. If the value of expression is NULL, IS NOT NULL returns FALSE; otherwise, it returns TRUE.

How do you set a field as NULL?

You can use this query, to set the specific row on a specific column to null this way: Update myTable set MyColumn = NULL where Field = Condition. Here, the above code will set the specific cell to null as per the inner question (i.e. To clear the value from a cell and make it NULL).


2 Answers

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

MSDN says

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

so you don't have to mention it explicitly

like image 175
Pரதீப் Avatar answered Sep 18 '22 07:09

Pரதீப்


Your syntax is wrong. By default all variables are nullable

 declare @myValue int=NULL

OUTPUT

 select @myValue
 NULL
like image 29
Ganesh Avatar answered Sep 22 '22 07:09

Ganesh