Technical Article

Script to generate the SQL to create a fulltext catalog and its underlying fulltext index

,

This script was published originally on http://humakhurshid.blog.com/2011/07/14/script_out_fulltext_catalog/ I have tweaked it little bit to improve it.

/*
Details: SQL script to generate the SQL required to create fulltext catalog
and all fulltext indexes within that catalog.
*//*
This script was published originally on http://humakhurshid.blog.com/2011/07/14/script_out_fulltext_catalog/ 
I have tweaked it little bit to improve it.
*//* Execute the script in the database for which you want to generate the FullText catalog script*/
DECLARE @Catalog NVARCHAR(128),
@SQL NVARCHAR(MAX),
@COLS NVARCHAR(4000),
@Owner NVARCHAR(128),
@Table NVARCHAR(128),
@ObjectID INT,
@AccentOn BIT,
@CatalogID INT,
@IndexID INT,
@Max_objectId INT,
@NL CHAR(2),
@i int

SET @i = 1;

-- Cursor to fetch the name of catalogs one by one for the current database

declare FTCur cursor for SELECT Name
FROM sys.fulltext_catalogs
ORDER BY NAME

OPEN FTCur
 

FETCH FTCur INTO @Catalog

WHILE @@FETCH_status >= 0

BEGIN

PRINT '**************************************************'
PRINT @i
SET @i = @i + 1;
PRINT '**************************************************'

SELECT
@NL = CHAR(13) + CHAR(10) --Carriage Return

-- Check catalog exists
IF EXISTS
(
SELECT Name
FROM sys.fulltext_catalogs
WHERE Name = @Catalog
) BEGIN
-- Store the catalog details
SELECT
@CatalogID = i.fulltext_catalog_id
,@ObjectID = 0
,@Max_objectId = MAX(object_id)
,@AccentOn = is_accent_sensitivity_on
FROM sys.fulltext_index_catalog_usages AS i
JOIN sys.fulltext_catalogs c
ON i.fulltext_catalog_id = c.fulltext_catalog_id
WHERE c.Name = @Catalog
GROUP BYi.fulltext_catalog_id
,is_accent_sensitivity_on

-- Script out catalog
PRINT 'CREATE FULLTEXT CATALOG ' + @Catalog + @NL
PRINT 'WITH ACCENT_SENSITIVITY = ' + CASE @AccentOn
WHEN 1
THEN 'ON'
ELSE 'OFF'
END
PRINT 'GO'

END

DECLARE FTObject CURSOR FOR SELECTMIN(i.object_id) objectId
,u.name AS schemaName
,t.Name
,unique_index_id
,c.name as catalogueName
FROM sys.tables AS t
JOIN sys.schemas AS u
ON u.schema_id = t.schema_id
JOIN sys.fulltext_indexes i
ON t.object_id = i.object_id
JOIN sys.fulltext_catalogs c
ON i.fulltext_catalog_id = c.fulltext_catalog_id
WHERE 1 = 1 
AND c.Name = @Catalog
--AND i.object_id > @ObjectID
GROUP BYu.name
,t.Name
,unique_index_id
,c.name

OPEN FTObject

FETCH FTObject INTO @ObjectID, @Owner, @Table, @IndexID, @Catalog
-- Loop through all fulltext indexes within catalog

WHILE @@FETCH_status >= 0 
BEGIN

-- Script Fulltext Index
SELECT
@COLS = NULL
,@SQL = 'CREATE FULLTEXT INDEX ON ' + QUOTENAME(@Owner) + '.' + QUOTENAME(@Table) + ' (' + @NL
PRINT @NL
-- Script columns in index
SELECT
@COLS = COALESCE(@COLS + ',', '') + c.Name + ' Language ' + CAST(Language_id AS varchar) + ' ' + @NL
FROM sys.fulltext_index_columns AS fi
JOIN sys.columns AS c
ON c.object_id = fi.object_id
AND c.column_id = fi.column_id
WHERE fi.object_id = @ObjectID

-- Script unique key index
SELECT
@SQL = @SQL + @COLS + ') ' + @NL + 'KEY INDEX ' + i.Name + @NL +
'ON ' + @Catalog + @NL +
'WITH CHANGE_TRACKING ' + fi.change_tracking_state_desc + @NL + 'GO' + @NL
FROM sys.indexes AS i
JOIN sys.fulltext_indexes AS fi
ON i.object_id = fi.object_id
WHERE i.Object_ID = @ObjectID
AND Index_Id = @IndexID

-- Output script SQL
PRINT @SQL

FETCH FTObject INTO @ObjectID, @Owner, @Table, @IndexID,@Catalog
END
CLOSE FTObject;
DEALLOCATE FTObject;
FETCH FTCur INTO @catalog
END
CLOSE FTCur
DEALLOCATE FTCur

Rate

5 (2)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (2)

You rated this post out of 5. Change rating