SQL Server 2005 features an integrated full-text search engine, which several people have e-mailed me to ask about recently. (ColdFusion users can use
If you want to play with SQL Server 2005 full-text search capabilities, you need to do the following:
1) Once a database has been created, support for full-text must be enabled before any full-text operations can be performed. To enable full-text support the sp_fulltext_database stored procedure is used. This stored procedure updates the currently selected database, so be sure to USE the correct database before issuing this statement:
EXEC sp_fulltext_database 'enable';
If you are using SQL Server Management Studio and use the interactive New Database dialog to create your database, you can check the “Use full-text indexing” checkbox, which causes the above mentioned stored procedure to be automatically executed.
2) SQL Server stores full-text data in a catalog (a file that needs to be created). A single catalog can be used for multiple tables and indexes. If a catalog does not already exist, create one using CREATE FULLTEXT CATALOG:
CREATE FULLTEXT CATALOG my_catalog;
This creates a catalog named my_catalog in the default catalog location. To specify the actual file location the IN PATH attribute can be specified.
3) Once a catalog has been created you can define the actual full-text indexes for each table containing columns that you want searchable. Indexes are created using CREATE FULLTEXT INDEX like this:
CREATE FULLTEXT INDEX ON table(column)
KEY INDEX table_primary_key
When creating a full-text index you specify the table and column to be indexed as table(column). More than one column may be indexed if needed, to do this simply specify the column names (comma delimited). The key with which to uniquely identify rows is required, and so KEY INDEX is used to provide the name of the table’s primary key. And finally, the ON clause specifies the catalog to be used to store full-text data, and here the just created catalog is used (unless a default catalog has been defined).
Once set up, you’ll be ready to use the CONTAINS and FREETEXT functions in your WHERE clauses to perform full-text searching.