Check if an index exists in SQL Server


Many times we come across the scenario where we need to some code based on whether an index exists or not.

The following query may help you to check an index is exists in a particular table or not. Suppose we have a table like dbo.Table1 and it has index idx_Index, and in some scenarios, we need to check idx_Index exists or not.


System.index catalog view records for each Clustered and Non-Clustered indexes.  We can execute the following query to find out a particular index exists or not in a particular table.


IF EXISTS
(
  SELECT 1 FROM sys.indexes
  WHERE name='idx_Index' AND object_id = OBJECT_ID('dbo.Table1')
)
BEGIN
     PRINT 'Index is Exists'
END

Related Posts

How to drop a PostgreSQL database if there are active connections to it?

In PostgreSQL, you cannot drop a database if there are active connections to it. You must first terminate all connections to the database be...