Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Assignment, Conditions, and Expressions with Fields allowing SQL Null

This section described the behavior of SQL Null fields in

The following field definitions are used throughout the text:

Code Block
Define Field(#A) Type(*DEC) Length(9) Decimals(0) Input_Atr(ASQN) Default(*SQLNULL) 
Define Field(#B) Type(*DEC) Length(9) Decimals(0) Input_Atr(ASQN) Default(*SQLNULL)
Define Field(#C) Type(*DEC) Length(9) Decimals(0) Default(*NULL)


Also See

 ASQN (Allow SQL Nulls) attribute

Specifying Conditions and Expressions in the Technical Reference Guide.

Anchor
Assignment
Assignment
Assignment

Info
If you wish to ensure that SQL Null fields are handled via ANSI rules for assignment, enable the *STRICT_NULL_ASSIGN function option. This option causes a fatal error to occur at execution time if the source field is SQL Null and the target field does not have the ASQN attribute.

...


If the target field does not have the ASQN attribute, the behavior varies depending on whether the *STRICT_NULL_ASSIGN function option is enabled. By default, the *NULL value for the field type will be assigned to the target field. In the example below, as #C is a numeric field, it would be set  to zero. For a definition of what the *NULL value is for each of the field types refer to CHANGE Parameters.

Code Block
#C := #B

However, if *STRICT_NULL_ASSIGN has been enabled, and the example code above is executed when #B is SQL Null, a fatal error will occur as the target field does not support being set to SQL Null. When working with *STRICT_NULL_ASSIGN, the LANSA Developer must code carefully to protect against such runtime errors. For example:

...


You can also use the .AsValue intrinsic method to treat an SQL Null field as a different value. This is useful for mathematics and concatenation, where SQL Null or *NULL are not appropriate values. In the following example, we now get the result of 5 in #C if #B is SQL Null. However, if #B was 3, #C would be set to 15 because #B.AsValue only affects #B when it is SQL Null.

Code Block
#C := #B.AsValue( 1 ) * 5

...



Also See

*SQLNULL Keyword
Intrinsic Property .IsSqlNull
Intrinsic Method .AsValue

Anchor
Conditions
Conditions
Conditions

  • To test for SQL Null, you should use *IS *SQLNULL or *ISNOT *SQLNULL, or the Intrinsic Property .IsSqlNull.
  • When using IF_NULL or .IsNull, an SQL Null field will return FALSE.
  • Since SQL Null does not represent a value, when using an equality operator such as *EQ, *LE, *GT to compare fields, and one of the factors of the compare is SQL Null, the comparison will produce an SQL Null. When combined with *OR and *AND operators, an SQL Null factor will continue to produce an SQL Null. A conditional expression that produces an SQL Null will evaluate to false.
  • SQLNULL comparisons will always stay as SQLNULL if the SQLNULL value is true. That is, when an expression is testing an SQLNULL, and there IS an SQLNULL, the expression will keep the SQLNULL value. For these types of scenarios, the *ORIF boolean feature should be used.

...

  • If you want a condition to return TRUE for both Null and SQL Null, use the *ORIF boolean feature together with Intrinsic Property .IsSqlNull and  and Intrinsic Property .IsNull. The following condition on our sample field #A will return true if the field is zero or SQL Null.

...

  • If you want a condition to return TRUE for both SQL Null and some other value, use the Intrinsic Method .AsValue. The following condition will return true if the field is 1 or SQL Null.

...

Condition

Result

#A.IsSqlNull

TRUE

#A.IsNull

FALSE

(#A.IsSqlNull) *orif (#A.IsNull)

TRUE

IF_NULL(#A #B #C)

FALSE

IF_NULL(#C)

TRUE

#A.AsValue(*ZERO) *EQ *ZERO

TRUE

#A *EQ *ZERO

FALSE

#A *EQ #B

FALSE

#A *LE #B

FALSE

#B *EQ #C

FALSE

#B *LE #C

FALSE

Anchor
Expressions
Expressions
Expressions

When expressions are being evaluated, intermediate results retain the SQL Null state. They are ALWAYS strictly interpreted. For example, when #B is SQL Null,  the result of expression '#B + 1' is SQL Null. That is, SQL Null plus 1 is still SQL Null. This is independent of the attributes of any result field.

It is only when the result of the expression is assigned into the result field that a difference in behaviour behavior can occur. If the result of an expression is SQL Null, behaviour depends on whether the result field allows SQL Null and also on the function option *STRICT_NULL_ASSIGN. Refer to Assignment for details.

If you wish to change the value of SQL Null fields to something more appropriate, use Intrinsic Method .AsValue. For example, the result of expression '#B.AsValue(1) + 1' is 2 when #B is SQL Null (and 5 when #B is 4).

SQL Null Handling
 
 
 
 
 
 
 
 
 
 
 
 
 
[ Image Removed |../../index.htm#lansa/l4wtgu01_0255.htm]