Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Regular Expressions 2005.pdf
Скачиваний:
95
Добавлен:
17.08.2013
Размер:
25.42 Mб
Скачать

Chapter 16

Using The CONTAINS Predicate

The CONTAINS predicate, which is part of SQL Server Full-Text Search, is used in the WHERE clause of a Transact-SQL SELECT statement.

You will use the CONTAINS predicate on the titles table, paying particular attention to the title and notes columns. If you haven’t already looked at the content of those columns, you can use the following Transact-SQL code to view the relevant information:

USE pubs

SELECT title_id, title, notes FROM titles

Figure 16-22 shows the appearance after the preceding code has been run.

Figure 16-22

386

Regular Expression Functionality in SQL Server 2000

The following exercise searches for titles that contain the word computer.

Try It Out

The CONTAINS Predicate

1.Open Query Analyzer, if it is not already open, and type the following code into the query pane:

USE pubs

SELECT title_id, title, notes FROM titles

WHERE CONTAINS(title, ‘ “computer” ‘)

2.Press F5 or click the blue right-pointing arrow to run the code. Figure 16-23 shows the appearance after this step. Notice that now only three titles are displayed. As you see in Figure 16-23, each of the titles contains the word computer.

The CONTAINS predicate takes two arguments. The first argument is the name of the column of interest. The second argument is the full-text search condition. In this example, the full-text search condition is very simple, being a literal string.

You could have achieved similar, but not identical, results using LIKE, as in the following code:

USE pubs

SELECT title_id, title, notes FROM titles

WHERE title LIKE ‘%computer%’

Figure 16-23

387

Chapter 16

3.Type the preceding code into the query pane of the Query Analyzer, and press F5 or click the blue right-pointing arrow to run the code.

Five rows are returned. Three contain the word computer, two contain the plural form computers, and both match the pattern %computer%.

The CONTAINS predicate allows two terms to be searched for using AND logic. For example, suppose you want to find titles that contain both computer and psychology. You can use the following WHERE clause:

WHERE CONTAINS(title, ‘ “computer” AND “psychology” ‘)

4.Type the following code into the query pane:

USE pubs

SELECT title_id, title, notes FROM titles

WHERE CONTAINS(title, ‘ “computer” AND “psychology” ‘)

5.Press F5 or click the blue right-pointing arrow to run the code. Figure 16-24 shows the appearance after this step. Notice that only one row is displayed, whose title contains both the words computer and psychology.

Similarly, OR logic can be used.

Figure 16-24

388

Regular Expression Functionality in SQL Server 2000

6.Type the following code into the query pane:

USE pubs

SELECT title_id, title, notes FROM titles

WHERE CONTAINS(title, ‘ “computer” OR “busy” ‘)

7.Press F5 or click the blue right-pointing arrow to run the code. The three titles that contain the word computer are displayed together with the one title that contains the word busy.

You can also match words that begin with specified characters. For example, the following WHERE clause will cause titles that contain words beginning with computer to be displayed:

WHERE CONTAINS(title, ‘ “computer*” ‘)

In the data in the titles table, titles containing computer or computers will be displayed. Proximity can also be tested using the NEAR keyword.

8.Type the following code into the query pane:

USE pubs

SELECT title_id, title, notes FROM titles

WHERE CONTAINS(title, ‘ “computer” NEAR “phobic” ‘)

9.Press F5 or click the blue right-pointing arrow to run the code. The row that contains the title containing both computer and phobic is displayed.

Inflectional forms can be tested using the FORMSOF keyword. Inflectional forms include plurals.

10.Type the following code into the query pane:

USE pubs

SELECT title_id, title, notes FROM titles

WHERE CONTAINS(title, ‘ FORMSOF(INFLECTIONAL,computer)’)

11.Press F5 or click the blue right-pointing arrow to run the code. Figure 16-25 shows the results. Notice that titles containing computer or computers are displayed.

Full-text searching has enormous power, particularly when extensive textual data is being manipulated. The pubs database allows limited testing only of full-text search.

How It Works

The CONTAINS predicate looks for whole words, computer in this example:

WHERE CONTAINS(title, ‘ “computer” ‘)

Combinations of words can be searched for using the AND keyword inside the CONTAINS predicate:

WHERE CONTAINS(title, ‘ “computer” AND “psychology” ‘)

The OR keyword provides functionality similar to alternation in standard regular expressions:

WHERE CONTAINS(title, ‘ “computer” OR “busy” ‘)

389

Chapter 16

Figure 16-25

The CONTAINS predicate can match words that contain a specified character sequence using the * metacharacter:

WHERE CONTAINS(title, ‘ “computer*” ‘)

The CONTAINS predicate allows proximity search, which has some resemblance to standard lookaround:

WHERE CONTAINS(title, ‘ “computer” NEAR “phobic” ‘)

The INFLECTIONAL option uses knowledge of word forms to find related words while specifying only one form of the word:

WHERE CONTAINS(title, ‘ FORMSOF(INFLECTIONAL,computer)’)

This differs from standard regular expressions where character sequences are matched with no understanding of English word forms being necessary. The INFLECTIONAL keyword operates by using such knowledge of word forms in English.

390