How to handle null in the database
For beginners, it is a little troublesome to process the NULL data type in the database. In this articleArticleArticle
Let's talk about null. You will know how to know which functions can or cannot
Rational null
First, we must know that in VBScript, variant is the only data type.
Familiar with other languagesProgramDevelopers may feel a little uncomfortable. The advantage of using variant is that it is equivalent
Elasticity, because variant can store any data type, such as integers, strings, date and time, or even packets.
Including objects and arrays. However, elasticity has to be paid because the specified variant may be better than the specified special data.
The Type uses much memory.
There are two special subtypes in the variant data type: Empty and null. In fact
Sub-types may be inappropriate because they do not store certain values. When the data sub-type of a variable is empty
Or null. Their values are empty or null.
Empty
After a variable is declared, before it is specified with a value, the sub-type of the variable is empty,
In other words, empty is equivalent to 'uninitialized '. Let's take a look at the example below.
Dim vartest
Response. Write typename (vartest)
The execution result should be empty, so empty is the initial data subtype and initial value of a variable,
Empty only represents the state of a variable. Try the following example:
Dim vartest
Response. Write clng (vartest)
Response. Write CSTR (vartest)
The first line of the program will show 0, because empty is expressed as an integer is 0, the result of the second line of execution will be
Nothing is displayed, because when expressed as a string, empty is empty, or it can be said to be a string of zero length.
When a variable is specified with a value, it is no longer empty, it will be another child type, depending on the Data Type
The difference is that, of course, you can use the keyword empty to change the variable back to the empty subtype.
Vartest = empty
There are two ways to determine whether a variable is empty
If vartest = empty then
Response. write 'The variable is empty .'
End if
Or
If isempty (vartest) then
Response. write 'The variable is empty .'
End if
Null
The data subtype of null is similar to empty, but the difference is that empty indicates that a variable has not been
Initialization, that is, it has not been assigned any value, and a variable is null only when you specify it as null
Later. The most common chance of null is that when processing a database, when a field has no data,
Is null.
The method for specifying and determining null is similar to empty.
Vartest = NULL
However, you can only use the isnull () function to determine null, because null represents an invalid resource.
Expected, you can try the following example
Dim vartest
Vartest = NULL
If vartest = NULL then
Response. write 'The variable has a null value .'
End if
The results of execution do not show the variable has a null value. Determine whether a variable is
Null you should use the isnull () function
Dim vartest
Vartest = NULL
If isnull (vartest) then
Response. write 'The variable has a null value .'
End if
When processing NULL data retrieved from the database, you must note that null represents
It is invalid data. When some functions are dealing with mathematical operations, null may cause some troubles, such
Dim vartest
Vartest = NULL
Vartest = clng (vartest)
The execution result shows the error message of 'invaliduse of null'. Let's take a look at the example below.
Dim vartest
Dim lngtest
Vartest = NULL
Lngtest = 2 + vartest
Response. Write typename (lngtest)
You will find that null is added with 2 or null
Therefore, after you obtain data from the database, you should first use isnull () to determine whether the field is null, and then
Appropriate processing, such
Lngqty = Ors ('quantuty ')
If isnull (lngqty) then
Lngqty = 0
End if