Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Script Library
 Drop Index If it Exists

Author  Topic 

gregoryagu
Yak Posting Veteran

80 Posts

Posted - 2008-07-31 : 17:51:43
--Drops an Index if it exists.
Create PROCEDURE [dbo].[Utils_DropIndex]
(
@TableName varchar(128),
@IndexName varchar(128)
)
AS
BEGIN

Declare @SQL varchar(4000)
set @SQL = 'IF EXISTS (SELECT * FROM sys.indexes '
+ ' WHERE object_id = OBJECT_ID('''
+ @TableName + ''') AND name = ''' + @IndexName + ''' )'
+ ' BEGIN PRINT ''Dropping the Index:'' PRINT '''
+ @IndexName + ''' DROP INDEX ['
+ @IndexName + '] ON [' + @TableName + '] END ELSE BEGIN PRINT ''Index Not Found:'''
+ 'Print ''' + @IndexName + ''' END'


--Print @SQL

EXECUTE (@SQL)
END
   

- Advertisement -