Implementing Full Text Search with T-SQL Stored Procedures

July 16, 2002


Enabling Full Text search in T-SQL is usually neither as "popular" nor as easy as doing it with the Enterprise Manager, but it can be done. (For more information on full text search in SQL Server, see our article from Alexander Chigrik, Setting Up Full-text Search: A Step-by-step Guide.) Here are the steps you'll need to take in order to implement FTS in T-SQL:


--1  Enabling Full Text on the Database

EXEC sp_fulltext_database  'enable'

--2  Create the Catalog (if does not exist)

EXEC sp_fulltext_catalog   'MyCatalog','create'

--3  Add a Full Text Index on a Table

EXEC sp_fulltext_table     'Products', 'create', 'MyCatalog', 'pk_products'
EXEC sp_fulltext_table     'Categories', 'create', 'MyCatalog', 'pk_categories'

--4  Add a Column to the Full Text Index

EXEC sp_fulltext_column    'Products', 'ProductName', 'add'
EXEC sp_fulltext_column    'Categories', 'Description', 'add'

--5  Activate the Index

EXEC sp_fulltext_table     'Products','activate'
EXEC sp_fulltext_table     'Categories','activate'

--6  Start Full Population

EXEC sp_fulltext_catalog   'MyCatalog', 'start_full'

-- Usage in T-SQL (CONTAINS and FREETEXT Predicates)
-- Using the Index in T-SQL

USE Northwind
GO
SELECT ProductId, ProductName, UnitPrice
FROM Products
WHERE CONTAINS(
                ProductName, ' "sasquatch " OR "stout" '
                )
GO


USE Northwind
GO
SELECT CategoryName
FROM Categories
FREETEXT (
           Description, 'sweetest candy bread and dry meat'
           )
GO


» See All Articles by Columnist Eli Leiba









The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers