Unlike programming languages such as C #, Java, in SQL Server, the initial value of a numeric type such as int is null, and the assignment operator can be assigned null, and a variable with a value of NULL cannot display its value in the message bar, only null is displayed in the result bar.
At the same time, the option to create a database causes the concatenated result to be null when the character data is concatenated with a "+" variable that has a null value.
1 DECLARE @ID INT;2 SET @ID=NULL;3 --output with "+" link null value and non-null value4 PRINT 'first output @id start'5 PRINT 'the values for the current @id are:'+CONVERT(NVARCHAR( -),@ID)6 PRINT 'First output @id complete'7 --output null and non-null values separately8 PRINT 'second output @id start'9 PRINT 'the values for the current @id are:'Ten PRINT CONVERT(NVARCHAR( -),@ID) One PRINT 'Second output @id complete' A --determine if @id is null by the IF decision - IF @ID is NULL - BEGIN the SELECT @ID asID; - PRINT 'the values for the current @id are:'+'NULL' - END - ELSE + BEGIN - PRINT 'the values for the current @id are:'+CONVERT(NVARCHAR( -),@ID) + END A PRINT 'assigns a value of 0 to @id' at --assigns a non-null value to the @id and outputs - SET @ID = 0 - PRINT 'the values for the current @id are:'+CONVERT(NVARCHAR( -),@ID) - GO
Sample Code
The first output @ID start the first output @ID finish The second output @ID start the current @ID value is : The second output @ID complete (1 rows affected) The value of the current @ID is :NULL for @ID is assigned a value of 0 Current the @ID value is :0
Output Results
Memory Essentials:
1. Unlike languages such as C #, the link string in SQL must convert non-character variables to type, otherwise an error will be made;
2. The result of a character type and a null connection may be null;
The initial value of the 3.SQL numeric variable is null;
4.SQL numeric variables can be assigned to null values directly with assignment operations;
5. Another: The value of the variable is null when a select is assigned and no record is queried.
"SQL Server Notes" nullability of SQL Server numeric types