The following tSQLt test checks that all the indexes exist & are enabled as expected:
CREATE PROCEDURE testSchema.[test that all indexes exist]
AS
BEGIN
IF OBJECT_ID('actual') IS NOT NULL DROP TABLE actual;
IF OBJECT_ID('expected') IS NOT NULL DROP TABLE expected;
CREATE TABLE actual (
name SYSNAME NOT NULL
,type_desc NVARCHAR(60) NOT NULL
,is_disabled BIT NOT NULL
,table_name SYSNAME NOT NULL
);
-- Update DatabaseName to the appropriate name for the data warehouse
INSERT INTO actual (name, type_desc, is_disabled, table_name)
SELECT i.name, i.type_desc, i.is_disabled, o.name AS table_name
FROM DatabaseName.sys.indexes i
INNER JOIN DatabaseName.sys.objects o
ON i.object_id = o.object_id;
-- Create a table with the expected index data to test against the actual sys.indexes table
CREATE TABLE expected (
name SYSNAME NOT NULL
,type_desc NVARCHAR(60) NOT NULL
,is_disabled BIT NOT NULL
,table_name SYSNAME NOT NULL
);
INSERT INTO expected (name, type_desc, is_disabled, table_name)
VALUES ('PK_dimDate', 'CLUSTERED', 0, 'dimDate')
,('IX_dimDate_Year', 'NONCLUSTERED', 0, 'dimDate');
EXEC tSQLt.AssertEqualsTable
@Expected = 'expected',
@Actual = 'actual';
END
See Misusing tSQLt for more info.
2 Comments
[…] Misusing tSQLt: test that indexes exist […]
Thank you … just what I needed