Vous êtes sur la page 1sur 1

80 Chapter 3: Selecting

SELECT DISTINCT
TOP 4 START AT 2
t1.key_1 * 100 AS a,
t3.key_1 * 1000 AS b,
COUNT(*) AS c,
SUM ( t3.non_key_1 ) AS d
INTO #t
FROM ( t1 LEFT OUTER JOIN t2 ON t1.key_1 = t2.key_1 )
CROSS JOIN t3
WHERE b <= 5000
AND t3.non_key_1 = 333
GROUP BY ROLLUP ( t1.key_1, t3.key_1 )
HAVING COUNT(*) > 1
ORDER BY 1, 2
FOR XML AUTO;
This book doesnt go into the details of XML processing. However, here is what
the single column in the single row in the temporary #t looks like, wrapped to fit
the page:
<t1 a="100"><t3 c="2" d="666"/></t1><t1 a="200"><t3 c="6" d="1998"/><t3 b="3000"
c="3" d="999"/><t3 b="4000" c="3" d="999"/></t1>'
For more information about the SELECT INTO method of creating a temporary
table, see Section 1.15.2.3, SELECT INTO #table_name.

3.3 FROM Clause


Logically speaking, the FROM clause specifies a virtual table or candidate
result set on which all the other clauses operate. Once upon a time the FROM
clause was just a simple list of table names, but no more; modern advances have
added complex table expressions with nested operations and boolean expres-
sions that once were found only in the WHERE clause.
Here is the basic syntax for the FROM clause:
<from_clause> ::= FROM <table_specification>
<table_specification> ::= <table_expression_list>
<table_expression_list> ::= <table_expression>
{ "," <table_expression> } -- avoid the comma
The FROM clause consists of a comma-separated list of table expressions,
which in turn may consist of nested table expressions, subqueries, and even lists
of table expressions inside brackets:
<table_expression> ::= <table_term>
| <table_expression>
CROSS JOIN
<table_term>
| <table_expression>
[ <on_condition_shorthand> ] -- do not use
<join_operator>
<table_term>
[ <on_condition> ] -- use this instead
<table_term> ::= <table_reference>
| <view_reference>
| <derived_table>
| <procedure_reference>
| "(" <table_expression_list> ")"
| <lateral_derived_table>
<on_condition_shorthand> ::= KEY -- foreign key columns; do not use
| NATURAL -- like-named columns; do not use

Vous aimerez peut-être aussi