USE [Arizona]

DECLARE @idxName VARCHAR(100) = 'NCIX_entry_COL_ET_reconciliation_status'
DECLARE @idxChk TABLE ([TblName] NVARCHAR(128),
                       [SchemaTbl] NVARCHAR(257),
                       [IndexName] NVARCHAR(128),
                       [IsPrimaryKey] BIT,
                       [IsUniqueConstraint] BIT,
                       [IsUnique] BIT,
                       [ColName] NVARCHAR(128),
                       [IsComputedCol] BIT,
                       [IsIncludedCol] BIT,
                       [keyOrdinal] TINYINT,
                       [FilterDefinition] NVARCHAR(MAX));

INSERT INTO @idxChk ([TblName],
                     [SchemaTbl],
                     [IndexName],
                     [IsPrimaryKey],
                     [IsUniqueConstraint],
                     [IsUnique],
                     [ColName],
                     [IsComputedCol],
                     [IsIncludedCol],
                     [keyOrdinal],
                     [FilterDefinition])
SELECT      [o].[name] AS [TblName],
            [s].[name] + '.' + [o].[name] AS [SchemaTbl],
            [i].[name] AS [IndexName],
            [i].[is_primary_key] AS [IsPrimaryKey],
            [i].[is_unique_constraint] AS [IsUniqueConstraint],
            [i].[is_unique] AS [IsUnique],
            [c].[name] AS [ColName],
            [c].[is_computed] AS [IsComputedCol],
            [ic].[is_included_column] AS [IsIncludedCol],
            [ic].[key_ordinal] AS [keyOrdinal],
            [i].[filter_definition] AS [FilterDefinition]
  FROM      [sys].[objects] [o]
 INNER JOIN [sys].[schemas] [s]
    ON [o].[schema_id] = [s].[schema_id]
 INNER JOIN [sys].[columns] [c]
    ON [o].[object_id] = [c].[object_id]
 INNER JOIN [sys].[indexes] [i]
    ON [c].[object_id] = [i].[object_id]
 INNER JOIN [sys].[index_columns] [ic]
    ON [i].[index_id]  = [ic].[index_id]
   AND [o].[object_id] = [ic].[object_id]
   AND [c].[column_id] = [ic].[column_id]
 WHERE      [i].[name]				  = @idxName
   AND      [c].[is_computed]         = 0
   AND      [ic].[is_included_column] = 0;


IF EXISTS(SELECT 1 FROM @idxChk [ic])	-- 'index exists
BEGIN
	IF EXISTS( SELECT 1 FROM @idxChk [ic] WHERE [ic].[ColName]='ET_reconciliation_status' AND [ic].[keyOrdinal]=1)
	AND EXISTS(SELECT 1 FROM @idxChk [ic] WHERE [ic].[ColName]='ET_account' AND [ic].[keyOrdinal] = 2)
	AND EXISTS(SELECT 1 FROM @idxChk [ic] WHERE [ic].[ColName]='ET_document_header' AND [ic].[keyOrdinal] = 3)
	BEGIN
		PRINT 'index '+@idxName+' is as expected';
	END
	ELSE 
	BEGIN 
		PRINT 'DROP INDEX '+@idxName+' ON dbo.[Entry];';
		--EXEC('DROP INDEX '+@idxName+' ON dbo.[Entry];')
	END
END 

IF INDEXPROPERTY(OBJECT_ID('dbo.Entry'), 'NCIX_entry_COL_ET_reconciliation_status', 'IndexID') IS NULL
BEGIN
    PRINT 'idx NCIX_entry_COL_ET_reconciliation_status create';
    --CREATE NONCLUSTERED INDEX NCIX_Entry_COL_ET_reconciliation_status
    --ON [dbo].[Entry] ([ET_reconciliation_status], [ET_account], [ET_document_header])
    --INCLUDE ([ET_debit_base_amount], [ET_credit_base_amount], [ET_reconciliation_base_amount]);
END
GO