Dropping an Index

  • Dear Forum,

    I want to do the equivalent of:

    If Object_ID('Table1') is not NULL Drop Table Table1;

    for indices.

    I have tried:

    If Object_ID('Table1.Table1_Index_1') is not NULL Drop Index Table1.Table1_Index_1;

    And

    If Object_ID('Table1_Index_1') is not NULL Drop Index Table1.Table1_Index_1;

    and neither works, although the simple drop does work providing the index exists.

    Any ideas what I'm doing wrong?

    Thanks, Bill

  • A dirty workaround might be:

    USE PUBS

    IF EXISTS(SELECT * FROM sysindexes WHERE [name] = 'PK_emp_id')

     Do something...

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • You could also use this is the index is not the primary key :

    Select * from dbo.SysIndexes where id = object_id('TableName') and name = 'IndexName'

  •  Thanks guys this method does the job. I don't mind dirty!

    Cheers, Bill

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply