How to correctly handle NULL in the database

xiaoxiao2021-03-06  61

For beginners, there is a bit of trouble in processing the NULL data in the database. In this article article, we will talk about null, you will know how to know that a value is null, which functions can or not Processing NULL First, we must know that in VBScript, Variant is the only data type, which may feel a bit not used to those program developers who are familiar with other languages. The advantage of using Variant is that it is quite flexible because Variant can store any data type, such as an integer, string, datetime, and even objects and arrays. However, elasticity is required to pay, because the specified variant may be much more than the memory used by the specified special data type.

There are also two very special subtypes (Subty): Empty and NULL in the Variant data type, in fact, the type of subtypes may not be appropriate, because they don't store some values, when a variable is used for EMPTY Or NULL, their value is EMPTY or NULL

EMPTY

A variable is after being declared, but before it is specified, the data subtype of this variable is EMPTY. In other words, EMPTY is equivalent to "not initialized", let's take a look at the example below.

DIM VARTEST

Response.write Typename (Vartest)

Its execution should be Empty, so EMPTY can be said to be a variable initial data sub-type and initial value, and EMPTY only represents a state of a variable, try the following example

DIM VARTEST

Response.write clng (VARTEST)

Response.write cstr (varteest)

The first line of the program will display 0, because Empty is expressed as an integer is 0, the result of the second line execution will not be displayed, because when the EMPTY is EMPTY when it is represented as a string, or it can be said to be the length is Zero string

When a variable is specified, it is no longer EMPTY, which will be other subtypes, depending on the type of information, of course, you can also use the Empty this variable back to EMPTY Subtype

Vartest = EMPTY

There are two ways you can judge 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

NULL is similar to EMPTY, but different points lies in Empty representing a variable has not been initialized, that is, the value has not been given any value, and a variable is NULL only after you specify it to null. The most often encountered NULL should be when the database is handled, when a field is not available, it is NULL.

Specifying and determining NULL method is similar to EMPTY

Vartest = NULL

However, you can only use the isnull () function to determine NULL, because NULL represents the illegal information, you can try the following examples

DIM VARTEST

Vartest = NULL

IF vartest = null dam

Response.write "The Variable Has A Null Value."

END IF

The result of the execution does not display the Variable Has A Null Value. To determine if 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 you are processed by NULL taken in the database, you have to pay attention, because NULL represents illegal information, null may create some trouble when some functions are processed, NULL may create some troubles, for example DIM VARTEST

Vartest = NULL

VARTEST = CLNG (VARTEST)

Executive result You will see the "Invalid Use of Null" error message, then look at the example below

DIM VARTEST

Dim lngtest

Vartest = NULL

LNGTEST = 2 VARTEST

Response.write Typename (LNGTEST)

You will find that NULL plus 2 or null therefore, when you get information from the database, you should use Isnull () to determine if the field is NULL, then properly process, for example

LNGQTY = ORS ("Quantuty")

IF isnull (lngqty) THEN

LNGQTY = 0

END IF

I hope this article helps you!

转载请注明原文地址:https://www.9cbs.com/read-112144.html

New Post(0)