Você está na página 1de 4

A.

Using CONTAINS with <simple_term>


The following example finds all products with a price of $80.99 that contain the word "Mountain".
USE AdventureWorks2008R2;
GO
SELECT Name, ListPrice
FROM Production.Product
WHERE ListPrice = 80.99
AND CONTAINS(Name, 'Mountain');
GO
B. Using CONTAINS and phrase in <simple_term>
The following example returns all products that contain either the phrase "Mountain" or "Road".
USE AdventureWorks2008R2;
GO
SELECT Name
FROM Production.Product
WHERE CONTAINS(Name, ' "Mountain" OR "Road" ')
GO
C. Using CONTAINS with <prefix_term>
The following example returns all product names with at least one word starting with the prefix chain in
the Name column.
USE AdventureWorks2008R2;
GO
SELECT Name
FROM Production.Product
WHERE CONTAINS(Name, ' "Chain*" ');
GO
D. Using CONTAINS and OR with <prefix_term>
The following example returns all category descriptions containing strings with prefixes of
either "chain" or "full".
USE AdventureWorks2008R2;
GO
SELECT Name
FROM Production.Product
WHERE CONTAINS(Name, '"chain*" OR "full*"');
GO
E. Using CONTAINS with <proximity_term>
The following example returns all product names that have the word bike near the word performance.
USE AdventureWorks2008R2;
GO
SELECT Description
FROM Production.ProductDescription
WHERE CONTAINS(Description, 'bike NEAR performance');
GO
F. Using CONTAINS with <generation_term>
The following example searches for all products with words of the form ride: riding, ridden, and so on.
USE AdventureWorks2008R2;
GO
SELECT Description
FROM Production.ProductDescription
WHERE CONTAINS(Description, ' FORMSOF (INFLECTIONAL, ride) ');
GO
G. Using CONTAINS with <weighted_term>
The following example searches for all product names containing the words performance, comfortable,
or smooth, and different weightings are given to each word.
USE AdventureWorks2008R2;
GO
SELECT Description
FROM Production.ProductDescription
WHERE CONTAINS(Description, 'ISABOUT (performance weight (.8),
comfortable weight (.4), smooth weight (.2) )' );
GO
H. Using CONTAINS with variables
The following example uses a variable instead of a specific search term.
USE AdventureWorks2008R2;
GO
DECLARE @SearchWord nvarchar(30)
SET @SearchWord = N'Performance'
SELECT Description
FROM Production.ProductDescription
WHERE CONTAINS(Description, @SearchWord);
GO
------------------------------------------------------------------------------
A. Returning rank values using CONTAINSTABLE
The following example searches for all product names containing the words breads, fish, or beers, and
different weightings are given to each word. For each returned row matching this search criteria, the relative
closeness (ranking value) of the match is shown. In addition, the highest ranking rows are returned first.
USE Northwind;
GO
SELECT FT_TBL.CategoryName, FT_TBL.Description, KEY_TBL.RANK
FROM Categories AS FT_TBL
INNER JOIN CONTAINSTABLE(Categories, Description,
'ISABOUT (breads weight (.8),
fish weight (.4), beers weight (.2) )' ) AS KEY_TBL
ON FT_TBL.CategoryID = KEY_TBL.[KEY]
ORDER BY KEY_TBL.RANK DESC;
GO
B. Returning rank values greater than specified value using
CONTAINSTABLE
The following example returns the description and category name of all food categories for which
the Description column contains the words "sweet and savory" near either the word sauces or
the word candies. All rows that have a category name Seafood are disregarded. Only rows with a rank
value of 2 or higher are returned.
USE Northwind;
GO
SELECT FT_TBL.Description, FT_TBL.CategoryName, KEY_TBL.RANK
FROM Categories AS FT_TBL
INNER JOIN CONTAINSTABLE (Categories, Description,
'("sweet and savory" NEAR sauces) OR
("sweet and savory" NEAR candies)'
) AS KEY_TBL
ON FT_TBL.CategoryID = KEY_TBL.[KEY]
WHERE KEY_TBL.RANK > 2
AND FT_TBL.CategoryName <> 'Seafood'
ORDER BY KEY_TBL.RANK DESC;
GO
C. Returning top 10 ranked results using CONTAINSTABLE and
top_n_by_rank
The following example returns the description and category name of the top 10 food categories where
the Description column contains the words "sweet and savory" near either the word "sauces" or the
word "candies".
USE Northwind;
SELECT FT_TBL.Description, FT_TBL.CategoryName , KEY_TBL.RANK
FROM Categories AS FT_TBL
INNER JOIN CONTAINSTABLE (Categories, Description,
'("sweet and savory" NEAR sauces) OR
("sweet and savory" NEAR candies)', 10)
AS KEY_TBL
ON FT_TBL.CategoryID = KEY_TBL.[KEY]
GO
-----------------------------------------------------------------------------

A. Using FREETEXT to search for words containing specified character


values
The following example searches for all documents containing the words related to "vital", "safety", and
"components".
USE AdventureWorks2008R2;
GO
SELECT Title
FROM Production.Document
WHERE FREETEXT (Document, 'vital safety components' );
GO
B. Using FREETEXT with variables
The following example uses a variable instead of a specific search term.
USE AdventureWorks2008R2;
GO
DECLARE @SearchWord nvarchar(30);
SET @SearchWord = N'high-performance';
SELECT Description
FROM Production.ProductDescription
WHERE FREETEXT(Description, @SearchWord);
GO

The following example returns the category name and description of all categories that relate
to sweet, candy, bread, dry, or meat.

USE Northwind;
SELECT FT_TBL.CategoryName
,FT_TBL.Description
,KEY_TBL.RANK
FROM dbo.Categories AS FT_TBL
INNER JOIN FREETEXTTABLE(dbo.Categories, Description,
'sweetest candy bread and dry meat') AS KEY_TBL
ON FT_TBL.CategoryID = KEY_TBL.[KEY];
GO

The following example is identical and shows the use of


the LANGUAGE language_term and top_n_by_rank parameters.
USE Northwind;
SELECT FT_TBL.CategoryName
,FT_TBL.Description
,KEY_TBL.RANK
FROM dbo.Categories AS FT_TBL
INNER JOIN FREETEXTTABLE(dbo.Categories, Description,
'sweetest candy bread and dry meat',LANGUAGE 'English',2)
AS KEY_TBL
ON FT_TBL.CategoryID = KEY_TBL.[KEY];
GO

Você também pode gostar