Sie sind auf Seite 1von 2

Accessing and Changing Relational Data (SQL Server 2000)

Using the Select List


The select list defines the columns in the result set of a SELECT statement The select list is a series of
e!"ressions se"arated #$ commas Each e!"ression defines a column in the result set The columns in
the result set are in the same order as the se%uence of e!"ressions in the select list
These attri#utes of the result set columns are defined #$ the e!"ressions in the select list&

The data t$"e' si(e' "recision' and scale of the result set column are the same as those of the
e!"ression defining the column

The name of the result set column is the name associated )ith the e!"ression defining the
column The o"tional AS *e$)ord can #e used to change the name' or to assign a name if the
e!"ression has no name

The data values for the result set column are derived from the evaluation of the e!"ression for
each ro) of the result set
The select list can also contain *e$)ords controlling the final format of the result set&

D+ST+,CT
The D+ST+,CT *e$)ord eliminates du"licate ro)s from a result set -or e!am"le' there are
man$ ro)s in the Northwind Orders ta#le )ith the same value for ShipCity To get a list of
the ShipCity values )ith du"licates removed&
SELECT DISTINCT ShipCity, ShipRegion
FROM Orders
ORDER BY ShipCity

T./ n
The T./ *e$)ord s"ecifies that the first n ro)s of the result set are returned +f .RDER 01 is
s"ecified' the ro)s are selected after the result set is ordered n is the num#er of ro)s to
return' unless the /ERCE,T *e$)ord is s"ecified /ERCE,T s"ecifies that n is the "ercentage of
ro)s in the result set that are returned -or e!am"le' this SELECT statement returns the first
20 cities' in al"ha#etic se%uence' from the Orders ta#le&
SELECT DISTINCT TOP 10 ShipCity, ShipRegion
FROM Orders
ORDER BY ShipCity
The items in the select list can include&

A sim"le e!"ression& a reference to a function' a local varia#le' a constant' or a column in a


ta#le or vie)

A scalar su#%uer$' )hich is a SELECT statement that evaluates to a single value for each result
set ro)

A com"le! e!"ression #uilt #$ using o"erators on one or more sim"le e!"ressions

The 3 *e$)ord' )hich s"ecifies that all columns in a ta#le are returned

4aria#le assignment in the form @local_variable 5 e!"ression The SET @local_variable


statement can also #e used for varia#le assignment

The +DE,T+T1C.L *e$)ord' )hich is resolved as a reference to the column in the ta#le having
the +DE,T+T1 "ro"ert$ -or e!am"le' the +DE,T+T1 "ro"ert$ has #een defined for the OrderID
column in the Northwind Orders ta#le' so the e!"ression .rders +DE,T+T1C.L is e%ual to
Orders.OrderID

The R.678+LDC.L *e$)ord' )hich is resolved as a reference to the column in a ta#le having
the R.678+DC.L "ro"ert$

Creating a ne) column (using SELECT +,T.) that uses the +DE,T+T1 "ro"ert$ #$ using the
s"ecified s$nta! -or e!am"le' to create a ne) column named counter in the authors ta#le
that is an int column' $ou should start at a value of 200 and increment #$ values of 2 for each
succeeding num#er' use counter 5 +DE,T+T1(int' 200' 2)

Tem"oraril$ adding a column to the %uer$ results that designates )hether the C80E or R.LL8/
o"eration added the ro) or not 8se the 7R.8/+,7 *e$)ord
This e!am"le sho)s man$ of the items that can #e in a select list&
SELECT FirstN!e " # # " LstN!e $S %E!p&oyee N!e%,
IDENTITYCOL $S %E!p&oyee ID%,
'o!ePhone,
Region,
10 $S Constnt
FROM North(ind)d*o)E!p&oyees
ORDER BY LstN!e, FirstN!e $SC
See Also
SELECT

Das könnte Ihnen auch gefallen