I wrote this code today.
Copy codeThe Code is as follows:
DECLARE @ recognition NVARCHAR (20)
SET @ ASD = NULL
IF (@ ASD = NULL)
BEGIN
PRINT 1
END
Originally, I wanted to print out 1. But no. Modify the Code as follows:
DECLARE @ recognition NVARCHAR (20)
SET @ ASD = NULL
IF (@ asd is null)
BEGIN
PRINT 1
END
In this way, 1 is printed correctly.
Then, modify the if statement
Copy codeThe Code is as follows:
IF (NULL = NULL)
BEGIN
PRINT 1
END
Result: 1 is not printed.
Therefore, we can sum up that the NULL value in the SQL statement is not equal to any value when performing the "=" operation. Use "is null" to determine whether the value is null.
We can also use the ISNULL function (@ str, 0) = 0 to determine whether the variable is NULL.
A small problem. record it, strengthen your memory, and be good at summing up.
========================================================== ==========================================================
Supplement: NULL and "+" operators.
"+" Can be used to connect strings and perform addition operations. What is the result of "+" between a NULL value and a string? I wrote the following verification program:
Copy codeThe Code is as follows:
DECLARE @ str NVARCHAR (200)
DECLARE @ str1 NVARCHAR (200)
DECLARE @ str2 NVARCHAR (200)
SET @ str = NULL
SET @ str1 = 'You look pretty today'
SET @ str2 = @ str + @ str1
PRINT @ str2
The results are not printed. Why? Let's test what is @ str2 now?
IF (@ str2 is null)
PRINT 1
The result is 1, so we can guess that NULL and any type of "+" operations still return NULL values.
Write another "+" verification program for the NULL and INT types:
Copy codeThe Code is as follows:
DECLARE @ num INT
DECLARE @ num1 INT
DECLARE @ num2 INT
SET @ num = 1
SET @ num1 = 2
SET @ num2 = @ num + @ num1
PRINT @ num2
3 is printed, and the red bold part is replaced with SET @ num = NULL. What is the result? Try it by yourself, haha.