How to check if a non-clustered index exists in SQL Server 2005

I have the following:

CREATE NONCLUSTERED INDEX [MyTableIndex] ON [dbo].[tablename] ([tablename_ID],[tablename_Field1]) INCLUDE ([Tablename_Field2],[Tablename_Field3]) 

I want to create an if statement to check if this exists. How to do it?

+46
sql-server-2005 non-clustered-index
May 03 '11 at 14:31
source share
2 answers
 IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'MyTableIndex' AND object_id = OBJECT_ID('tablename')) BEGIN -- Index with this name, on this table does NOT exist END 
+102
May 03 '11 at 14:35
source share

Try the following:

 IF NOT EXISTS(SELECT * FROM sys.indexes WHERE Name = 'MyTableIndex') -- put your CREATE INDEX statement here 
+13
May 03 '11 at 14:35
source share



All Articles