开发者

How do I check if a nonclustered index exists in SQL Server 2005

开发者 https://www.devze.com 2023-03-03 01:51 出处:网络
I have the following: CREATE NONCLUSTERED INDEX [MyTableIndex] ON [dbo].[tablename] ([tablename_ID],[tablename_Field1])

I have the following:

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

I want to cr开发者_运维知识库eate an if statement to check if this exists. How do I do this?


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


Try this:

IF NOT EXISTS(SELECT * FROM sys.indexes WHERE Name = 'MyTableIndex')
   -- put your CREATE INDEX statement here
0

精彩评论

暂无评论...
验证码 换一张
取 消