Indexing Service: SQL Query Syntax Tips

Tips for Searching using SQL

### The SELECT Statement

Use the SELECT statement to retrieve rows. The SELECT statement consists of:

- the SELECT-list which identifies the columns (properties) of interest,
- the FROM clause which specifies the scope (the set of files) for the search, and
- the WHERE clause which is the search criteria.

If the results must be ordered, you can add an additional ORDER BY clause to return the rows in ascending or descending order.

Example

`SELECT DocAuthor, size, DocTitle
FROM SCOPE(' "/IISSamples/ISSamples" ') WHERE CONTAINS(' "index server" ') > 0 ORDER BY size`

#### The FROM Clause

The FROM clause of the SELECT statement is used to specify the files on which to perform the search; that is, use the FROM clause to define the query scope. You can use the SCOPE() function, which is the main component of the FROM clause. The SCOPE function can take zero or more comma-separated Scope_Arguments (that is, Traversal_Type and Path combinations). You can specify SCOPE with an empty argument list, or (). This is the default scope that uses the virtual root ( / ) as its path. Each Scope_Argument must be surrounded by single quotes (see Examples below).

In addition to using Scope(), you can also refer to any one of a set of predefined views of Indexing Service properties that are often queried against. You can reference one of these pre-defined views in the FROM clause by specifying the pre-defined View_Name.

Examples

`SELECT DocAuthor, size FROM SCOPE() WHERE size > 500000`

SELECT DocAuthor, size
FROM SCOPE(' SHALLOW TRAVERSAL OF "D:\Contracts\open" ',' DEEP TRAVERSAL OF "/Reports/Year 97" ')
WHERE CONTAINS(DocAuthor, ' "John" ') > 0

SELECT * FROM EXTENDED_WEBINFO
WHERE CONTAINS(DocSubject, ' "index server" NEAR() "internet information server" ') > 0

#### The WHERE Clause

The WHERE clause of the SELECT statement specifies which rows in the virtual table defined by the FROM clause make up the resulting rowset. The WHERE clause consists of one or more search conditions (that is, one or more predicates combined with AND, OR and NOT) that filter out rows for which the search condition is false.

Examples

`SELECT FileName, size FROM SCOPE() WHERE DocTitle = 'Financial Data' OR DocAuthor = 'John Smith'`

SELECT FileName, DocAuthor FROM FILEINFO WHERE size < 10000 OR DocWordCount <= 800

SELECT DocTitle, FileName, write FROM SCOPE()
WHERE CONTAINS (' "Index" NEAR() "Server" NEAR() "Microsoft"') > 0 AND size < 5000

### The ORDER BY Clause

The optional ORDER BY clause can be appended to the SELECT statement to sort the rows returned in the rowset according to a specified set of criteria. Results are sorted by default in ascending order. To sort in descending order, specify DESC after the column name.

Example ``

SELECT FileName, DocTitle, size, rank FROM SCOPE(' "/MyDocs/Specs97", "/YourDocs/Specs97" ')
WHERE FREETEXT (' "How do I index my HTML pages" ') > 0 ORDER BY rank, size DESC

For a more detailed description of the SQL syntax supported in Indexing Service, including use of the CONTAINS and FREETEXT predicates as illustrated in the examples on this page, please refer to the product documentation.

© 1997-2002 by Microsoft Corporation. All rights reserved.