Você está na página 1de 1

114 Chapter 3: Selecting

value, UNKNOWN, is necessary because the result of most comparisons


involving NULL are, in fact, unknown. For example, if X contains NULL, nei-
ther of the comparisons X = 0 or X <> 0 is TRUE. Neither of them is FALSE,
either; they both return UNKNOWN.

Note: You wont find boolean expression in the SQL Anywhere Help
look for search condition instead. The term search condition implies a repeti-
tive act, which may apply to the WHERE clause but not to a simple IF statement,
and thats why this book uses boolean expression instead.

Boolean expressions consist of the following:


n One or more predicates
n Boolean operators AND, OR, and NOT
n Truth value tests IS and IS NOT
n Parentheses to control the order of execution
n User estimates to influence the query optimizer
Predicates are the basic building blocks of a boolean expression; a predicate is
the simplest expression that yields TRUE, FALSE, or UNKNOWN as its result.
There are seven different kinds of predicates, each of which is discussed in the
next seven sections. For the purposes of this section, simple comparison predi-
cates of the form X = Y will be used to show how complex boolean
expressions can be constructed from multiple predicates. Here is a table show-
ing some simple predicates and the resulting truth values for different values
of X:
X contains:
=====================
Predicate Examples NULL 0 1
===================== ======= ===== =====
X = 0 UNKNOWN TRUE FALSE
X = 1 UNKNOWN FALSE TRUE
COALESCE ( X, 0 ) = 0 TRUE TRUE FALSE
COALESCE ( X, 0 ) = 1 FALSE FALSE TRUE
X <> 0 UNKNOWN FALSE TRUE
X <> 1 UNKNOWN TRUE FALSE
Heres how to read the first line of the table: If X contains NULL, the predicate
X = 0 returns UNKNOWN. If X contains 0, the predicate returns TRUE. If X
contains 1, the predicate returns FALSE.

Note: TRUE, FALSE, and UNKNOWN are actual SQL Anywhere 9 keywords
representing boolean or truth values. Unfortunately, however, there is no explicit
BOOLEAN or TRUTH data type in SQL Anywhere 9. You cant declare a variable
or column as BOOLEAN, so you cant directly store the value of a <boolean_
expression> in a variable or column. All you can do is use them as you calculate
them, in WHERE clauses, ON conditions, and so on. Or use some other data
type like BIT to hold 1 or 0, or VARCHAR ( 1 ) to hold 'Y' or 'N', and write code to
indirectly calculate, store, and use values of those types. The IF and SET state-
ments can be used for this purpose, and theyre discussed in Chapter 8,
Packaging.

Você também pode gostar