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
SELECT ProductId, ProductName, UnitPrice
FROM Products
                ProductName, ' "sasquatch " OR "stout" '

USE Northwind
SELECT CategoryName
FROM Categories
           Description, 'sweetest candy bread and dry meat'

» See All Articles by Columnist Eli Leiba