Following is an outline of the steps that an administrator operating in the “stored procedure mode” could take to set up full-text search on selected tables and columns in the pubs database. This scenario does not demonstrate everything that is available, but only enough to provide insight as to how these procedures can be used.
If necessary, the service can be started in one of several ways:
The full-text service also can be stopped in all of these places.
In this case, pubs has not been enabled.
SELECT DatabaseProperty('pubs', 'IsFulltextEnabled')
returns 1 if the service has been enabled, and 0 if it has not. In this case, the service has been enabled.USE pubs
sp_fulltext_database 'enable'
sp_fulltext_catalog 'PubsCatalog', 'create'
This procedure creates metadata about a full-text catalog in the system table of the database and builds an empty full-text catalog in the file system. The file is created in a default root directory that can be overridden, if desired, by using a third parameter.
To register such a table, you must specify the name of the index that enforces the unique value for the unique key column. For a given table, this information can be obtained using the sp_helpindex stored procedure. The indexes of interest in this scenario are:
authors | UPKCL_auidind |
jobs | PK_jobs_22AA996 |
pub_info | UPKCL_pubinfo |
titles | UPKCL_titleind |
Knowing these names, it is now possible to register the tables by invoking the sp_fulltext_table stored procedure once for each table:
sp_fulltext_table 'authors', 'create', 'PubsCatalog', 'UPKCL_auidind'
sp_fulltext_table 'jobs', 'create', 'PubsCatalog', 'PK_jobs_22AA996'
sp_fulltext_table 'pub_info', 'create', 'PubsCatalog', 'UPKCL_pubinfo'
sp_fulltext_table 'titles', 'create', 'PubsCatalog', 'UPKCL_titleind'
The effect of these invocations is to update the metadata in the system tables both for this full-text catalog and for these tables.
sp_fulltext_column 'authors', 'address', 'add'
sp_fulltext_column 'jobs', 'job_desc', 'add'
sp_fulltext_column 'pub_info', 'pr_info', 'add'
sp_fulltext_column 'titles', 'type', 'add'
sp_fulltext_column 'titles', 'notes', 'add'
The effect of these invocations is to augment the metadata in the system tables.
Note A mistake was made for the sake of later illustration. For the titles table, the type column, rather than the titles column has been registered.
sp_fulltext_table 'authors', 'activate'
sp_fulltext_table 'jobs', 'activate'
sp_fulltext_table 'pub_info', 'activate'
sp_fulltext_table 'titles', 'activate'
This does not create the full-text indexes; it only registers the tables as active in the metadata of the full-text catalog so that data from these tables will be included in the next population. Additionally, this defines full-text population start seeds for each table to the full-text service.
sp_fulltext_catalog 'PubsCatalog', 'start_full'
The population of a full-text catalog is an asynchronous operation. This means that immediately following the execution the procedure and the return to the procedure’s caller, it is unlikely that the full-text indexes will have been created yet.
SELECT FulltextCatalogProperty ('PubsCatalog', 'PopulateStatus')
returns 0 if the service is idle for the full-text catalog and therefore (supposedly) finished, and 1 or more to indicate various stages of population.SELECT P.pub_name, T.title, T.price
FROM publishers P, titles.T
WHERE P.pub_id = T.pub_id
AND P.country = 'England'
AND CONTAINS (T.notes,
'"case is altered" OR
"cat and custard pot" OR
"the monarch and the sphinx"
' )
SELECT title_id, title, pubdate
FROM titles
WHERE CONTAINS (T.title,
'classic ~ french ~ cooking')
This results in an error because the title column was not registered for full-text queries.
SELECT ColumnProperty ( ObjectId('titles'),
'titles',
'IsFulltextIndexed' )
In this case, the value returned is 0.
sp_help_fulltext_columns 'titles'
The results of this query will show that there was a mistake and that type instead of title was included in the full-text index definition.
sp_fulltext_table 'titles', 'deactivate'
In addition to allowing columns to be added and deleted, the effect of deactivating the titles table means that the table no longer participates in the population of the PubsCatalog full-text catalog. However, the metadata remains and the table can be reactivated. The existing full-text index for the titles table remains in place until the next full population of PubsCatalog, but it is unused because SQL Server blocks queries on deactivated tables.
sp_fulltext_column 'titles', 'type', 'drop'
sp_fulltext_column 'titles', 'title', 'add'
sp_fulltext_table 'titles', 'activate'
If the table is reactivated and the index is not repopulated, the old index is still available for queries against all the full-text registered columns that remain (but not against any new full-text registered columns). Data from deleted columns will be matched on queries that specify a * search ( that is, all full-text columns in a table).
sp_fulltext_catalog 'PubsCatalog',
'start_incremental'
An incremental population will refresh the full-text catalog by indexing data in the full-text enabled columns that meet any of the following criteria: