Implementing Full Text Search with T-SQL Stored Procedures


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

Eli Leiba
Eli works at Israel Electric Company as a Senior Application DBA in Oracle and MS SQL Server. He also has certifications from Microsoft and BrainBench in Oracle and SQL Server database administration and implementation. Mr. Leiba holds a B.S. in Computer Science since 1991 and has 11 years experience working in the database field. Additionally, Mr. Leiba teaches SQL Server DBA and Development courses at Microsoft CTEC and also serves as a senior database consultant for several Israeli start-up companies.

Latest Articles