-- Create temp table to hold index creation info
 
IF OBJECT_ID('tempdb..#FullTextIndexes') IS NOT NULL DROP TABLE #FullTextIndexes;
CREATE TABLE #FullTextIndexes (
    TableName NVARCHAR(256),
    Columns NVARCHAR(MAX),
    KeyIndex NVARCHAR(256)
);
 
-- Insert all index definitions
INSERT INTO #FullTextIndexes (TableName, Columns, KeyIndex) VALUES
('[dbo].[Company$Customer$437dbf0e-84ff-417a-965d-ed2bb9650972]', '[No_] LANGUAGE [Neutral], [Name] LANGUAGE [Neutral], [Name 2] LANGUAGE [Neutral], [Address] LANGUAGE [Neutral], [Address 2] LANGUAGE [Neutral], [City] LANGUAGE [Neutral], [Contact] LANGUAGE [Neutral], [Phone No_] LANGUAGE [Neutral], [Telex No_] LANGUAGE [Neutral], [Our Account No_] LANGUAGE [Neutral], [Fax No_] LANGUAGE [Neutral], [Telex Answer Back] LANGUAGE [Neutral], [VAT Registration No_] LANGUAGE [Neutral], [County] LANGUAGE [Neutral], [E-Mail] LANGUAGE [Neutral], [Home Page] LANGUAGE [Neutral], [Mobile Phone No_] LANGUAGE [Neutral], [Contact Graph Id] LANGUAGE [Neutral], [EORI Number] LANGUAGE [Neutral], [Registration Number] LANGUAGE [Neutral], [Format Region] LANGUAGE [Neutral]', '[Company$Customer$437dbf0e-84ff-417a-965d-ed2bb9650972$$systemId]'),
('[dbo].[Company$Item$437dbf0e-84ff-417a-965d-ed2bb9650972]', '[No_] LANGUAGE [Neutral], [Description] LANGUAGE [Neutral], [Description 2] LANGUAGE [Neutral], [Vendor Item No_] LANGUAGE [Neutral], [Block Reason] LANGUAGE [Neutral]', '[Company$Item$437dbf0e-84ff-417a-965d-ed2bb9650972$$systemId]'),
('[dbo].[Company$Vendor$437dbf0e-84ff-417a-965d-ed2bb9650972]', '[No_] LANGUAGE [Neutral], [Name] LANGUAGE [Neutral], [Name 2] LANGUAGE [Neutral], [Address] LANGUAGE [Neutral], [Address 2] LANGUAGE [Neutral], [City] LANGUAGE [Neutral], [Contact] LANGUAGE [Neutral], [Phone No_] LANGUAGE [Neutral], [Telex No_] LANGUAGE [Neutral], [Our Account No_] LANGUAGE [Neutral], [Fax No_] LANGUAGE [Neutral], [Telex Answer Back] LANGUAGE [Neutral], [VAT Registration No_] LANGUAGE [Neutral], [County] LANGUAGE [Neutral], [E-Mail] LANGUAGE [Neutral], [Home Page] LANGUAGE [Neutral], [Registration No_] LANGUAGE [Neutral], [EORI Number] LANGUAGE [Neutral], [Registration Number] LANGUAGE [Neutral], [Format Region] LANGUAGE [Neutral], [Mobile Phone No_] LANGUAGE [Neutral]', '[Company$Vendor$437dbf0e-84ff-417a-965d-ed2bb9650972$$systemId]'),
('[dbo].[Company$Contact$437dbf0e-84ff-417a-965d-ed2bb9650972]', '[No_] LANGUAGE [Neutral], [Name] LANGUAGE [Neutral], [Name 2] LANGUAGE [Neutral], [Address] LANGUAGE [Neutral], [Address 2] LANGUAGE [Neutral], [City] LANGUAGE [Neutral], [Phone No_] LANGUAGE [Neutral], [Telex No_] LANGUAGE [Neutral], [Fax No_] LANGUAGE [Neutral], [Telex Answer Back] LANGUAGE [Neutral], [VAT Registration No_] LANGUAGE [Neutral], [County] LANGUAGE [Neutral], [E-Mail] LANGUAGE [Neutral], [Home Page] LANGUAGE [Neutral], [Company Name] LANGUAGE [Neutral], [First Name] LANGUAGE [Neutral], [Middle Name] LANGUAGE [Neutral], [Surname] LANGUAGE [Neutral], [Job Title] LANGUAGE [Neutral], [Initials] LANGUAGE [Neutral], [Extension No_] LANGUAGE [Neutral], [Mobile Phone No_] LANGUAGE [Neutral], [Pager] LANGUAGE [Neutral], [E-Mail 2] LANGUAGE [Neutral], [Registration Number] LANGUAGE [Neutral], [Format Region] LANGUAGE [Neutral], [Business Relation] LANGUAGE [Neutral]', '[Company$Contact$437dbf0e-84ff-417a-965d-ed2bb9650972$$systemId]'),
('[dbo].[Company$Item Reference$437dbf0e-84ff-417a-965d-ed2bb9650972]', '[Item No_] LANGUAGE [Neutral], [Description] LANGUAGE [Neutral], [Description 2] LANGUAGE [Neutral]', '[Company$Item Reference$437dbf0e-84ff-417a-965d-ed2bb9650972$$systemId]'),
('[dbo].[Company$Serial No_ Information$437dbf0e-84ff-417a-965d-ed2bb9650972]', '[Item No_] LANGUAGE [Neutral], [Serial No_] LANGUAGE [Neutral], [Description] LANGUAGE [Neutral]', '[Company$Serial No_ Information$437dbf0e-84ff-417a-965d-ed2bb9650972$$systemId]'),
('[dbo].[Company$Lot No_ Information$437dbf0e-84ff-417a-965d-ed2bb9650972]', '[Item No_] LANGUAGE [Neutral], [Lot No_] LANGUAGE [Neutral], [Description] LANGUAGE [Neutral]', '[Company$Lot No_ Information$437dbf0e-84ff-417a-965d-ed2bb9650972$$systemId]'),
('[dbo].[Company$Service Item$437dbf0e-84ff-417a-965d-ed2bb9650972]', '[No_] LANGUAGE [Neutral], [Description] LANGUAGE [Neutral], [Description 2] LANGUAGE [Neutral], [Location of Service Item] LANGUAGE [Neutral], [Vendor Item Name] LANGUAGE [Neutral], [Sales_Serv_ Shpt_ Document No_] LANGUAGE [Neutral]', '[Company$Service Item$437dbf0e-84ff-417a-965d-ed2bb9650972$$systemId]'),
('[dbo].[Company$Item Variant$437dbf0e-84ff-417a-965d-ed2bb9650972]', '[Code] LANGUAGE [Neutral], [Description] LANGUAGE [Neutral], [Description 2] LANGUAGE [Neutral]', '[Company$Item Variant$437dbf0e-84ff-417a-965d-ed2bb9650972$$systemId]'),
('[dbo].[Company$Fixed Asset$437dbf0e-84ff-417a-965d-ed2bb9650972]', '[No_] LANGUAGE [Neutral], [Description] LANGUAGE [Neutral], [Description 2] LANGUAGE [Neutral], [Serial No_] LANGUAGE [Neutral]', '[Company$Fixed Asset$437dbf0e-84ff-417a-965d-ed2bb9650972$$systemId]'),
('[dbo].[Company$Stockkeeping Unit$437dbf0e-84ff-417a-965d-ed2bb9650972]', '[Item No_] LANGUAGE [Neutral], [Vendor Item No_] LANGUAGE [Neutral]', '[Company$Stockkeeping Unit$437dbf0e-84ff-417a-965d-ed2bb9650972$$systemId]'),
('[dbo].[Company$Nonstock Item$437dbf0e-84ff-417a-965d-ed2bb9650972]', '[Entry No_] LANGUAGE [Neutral], [Vendor No_] LANGUAGE [Neutral], [Description] LANGUAGE [Neutral]', '[Company$Nonstock Item$437dbf0e-84ff-417a-965d-ed2bb9650972$$systemId]'),
('[dbo].[Company$Post Code$437dbf0e-84ff-417a-965d-ed2bb9650972]', '[Code] LANGUAGE [Neutral], [City] LANGUAGE [Neutral], [County] LANGUAGE [Neutral]', '[Company$Post Code$437dbf0e-84ff-417a-965d-ed2bb9650972$$systemId]'),
('[dbo].[Company$G_L Account$437dbf0e-84ff-417a-965d-ed2bb9650972]', '[No_] LANGUAGE [Neutral], [Name] LANGUAGE [Neutral]', '[Company$G_L Account$437dbf0e-84ff-417a-965d-ed2bb9650972$$systemId]'),
('[dbo].[Company$Dimension Value$437dbf0e-84ff-417a-965d-ed2bb9650972]', '[Dimension Code] LANGUAGE [Neutral], [Name] LANGUAGE [Neutral], [Totaling] LANGUAGE [Neutral]', '[Company$Dimension Value$437dbf0e-84ff-417a-965d-ed2bb9650972$$systemId]')
 
DECLARE @TableName NVARCHAR(256), @Columns NVARCHAR(MAX), @KeyIndex NVARCHAR(256), @Sql NVARCHAR(MAX);
 
DECLARE IndexCursor CURSOR FOR
    SELECT TableName, Columns, KeyIndex FROM #FullTextIndexes;
 
OPEN IndexCursor;
FETCH NEXT FROM IndexCursor INTO @TableName, @Columns, @KeyIndex;
 
WHILE @@FETCH_STATUS = 0
BEGIN
    IF NOT EXISTS (SELECT 1 FROM sys.fulltext_indexes WHERE object_id = OBJECT_ID(@TableName))
    BEGIN
        SET @Sql = N'CREATE FULLTEXT INDEX ON ' + @TableName + ' (' + @Columns + ') KEY INDEX ' + @KeyIndex + ' ON [bc_ft_catalog];';
        EXEC sp_executesql @Sql;
        PRINT 'Created fulltext index on ' + @TableName;
    END
    ELSE
        PRINT 'Fulltext index already exists on ' + @TableName;
 
    FETCH NEXT FROM IndexCursor INTO @TableName, @Columns, @KeyIndex;
END
 
CLOSE IndexCursor;
DEALLOCATE IndexCursor;
 
DROP TABLE #FullTextIndexes;