Sie sind auf Seite 1von 1

Chapter 3: Selecting 103

| <number_literal>
| <special_literal>
| NULL
| <function_call>
<column_reference> ::= <column_name>
| <alias_name>
| [ <owner_name> "." ] <table_name> "." <column_name>
| <correlation_name> "." <column_name>
<variable_reference> ::= a reference to a SQL variable
<number_literal> ::= integer, exact numeric or float numeric literal
<special_literal> ::= see <special_literal> in Chapter 1, Creating
The syntax of an <expression> is more complex than it has to be to satisfy the
needs of a select list item. Thats because expressions can appear in many other
places in SQL, and some of these other contexts place limitations on what may
or may not appear in an expression. In particular, there are three kinds of
expressions defined above:
n First, there is the full-featured <expression>, which includes everything
SQL Anywhere has to offer. Thats the kind allowed in a select list, and
thats what this section talks about.
n The second kind is a <basic_expression>, which has everything an <expres-
sion> has except for subqueries. For example, a <case_expression> may
not have a subquery appearing after the CASE keyword, and thats one con-
text where <basic_expression> appears in the syntax.
n The third kind is a <simple_expression>, which is like a <basic_expres-
sion> except it cannot begin with the IF or CASE keywords. For example,
the message text parameter in the RAISERROR statement cant be any fan-
cier than a <simple_expression>.
In reality, these are extremely subtle differences, unlikely to get in your way.
From now on, as far as this book is concerned, an expression is just an expres-
sion and only the BNF will show the differences.

Tip: When using several arithmetic operators in a single expression, use


parentheses to make the order of calculation clear. The default order when
parentheses are not used is to perform multiplication and division first, and then
addition and subtraction. Not everyone knows this or remembers it, so parenthe-
ses are a good idea if you want your code to be readable.

Following is an example of a SELECT that contains only one clause, the select
list. The first and third expressions perform date arithmetic by subtracting one
day from and adding one day to the special literal CURRENT DATE to compute
yesterdays and tomorrows dates. The last four select list items are subqueries
that compute single values: the maximum value of product.unit_price, the num-
ber of rows in the product and sales_order tables, and the sum of all
sales_order_items.quantity values.
SELECT CURRENT DATE - 1 AS yesterday,
CURRENT DATE AS today,
CURRENT DATE + 1 AS tomorrow,
( SELECT MAX ( unit_price )
FROM product ) AS max_price,
( SELECT COUNT(*)
FROM product ) AS products,
( SELECT COUNT(*)

Das könnte Ihnen auch gefallen