views:

77

answers:

2

Hey guys, I am trying to work on the upgrade component in our program. This requires changing an index's name

EXEC sp_rename N'Sig_Summary1Index.IX_Sig_Summary1Index_StartTime',

N'Sig_Summary3Index.IX_Sig_Summary1Index1_StartTime', N'INDEX';

(we need to remain compatible with SQL SERVER 2005)

and then check if it exists in the same transaction:

IF NOT EXISTS 
(   SELECT * 
    FROM sys.indexes 
    WHERE object_id = OBJECT_ID(N'[dbo].[Sig_Summary1Index]' ) 
        AND name = N'[IX_Sig_Summary1Index1_StartTime]') 

CREATE CLUSTERED INDEX [IX_Sig_Summary1Index1_StartTime] 
ON [dbo].[Sig_Summary1Index] (  
    [StartTime] ASC 
)
WITH (  
    PAD_INDEX  = OFF, 
    STATISTICS_NORECOMPUTE  = OFF, 
    SORT_IN_TEMPDB = OFF, 
    IGNORE_DUP_KEY = OFF, 
    DROP_EXISTING = OFF, 
    ONLINE = OFF, 
    ALLOW_ROW_LOCKS  = ON, 
    ALLOW_PAGE_LOCKS  = ON, 
    FILLFACTOR = 95
) 
ON [PRIMARY]

This WILL try to create the index. :( somehow, during the transaction the sys.indexes table is not updated yet. Is there any other way to avoid creating this index?

Edit: Is there any way i can "flush" the sys tables to update? Insert the records manually? do something so that the "check" will not be changed?

A: 

I tend to check if the index exists with the old name, if it does drop it, GO

Re-create index with new name, GO.

It's simpler.

Mitch Wheat
Yeah, thats a trivial but very unclean solution. Is there any way i can "flush" the sys tables update?Update the record in the table manually?
Feel
Unclean? why? It's perfectly satisfactory
Mitch Wheat
I cannot change the 2nd function --> the createIndex one, If I do, that means i have to write a specific patch for this occasion and this I'm trying to avoid.
Feel
I've patched many DB's, the way I described.
Mitch Wheat
+1  A: 

sys.tables is always in perfect synch with CREATE INDEX. If your query doesn't find it means it looks for the wrong index name. If you run the CREATE INDEX fron inside a transaction and then, before commit, you look in sys.tables from another transaction, you fall into ordinary read isolation levels and you won't see the new index until the transaction commits. Which is the expected and desired behavior.

Remus Rusanu
Aren't all the Indexes listed in Sys.Indexes ? Is it also in sync with CREATE INDEX?I am trying to rename an index and then ask if it exists in the same transaction
Feel
they are all listed. If you look for an index created as `[foo]`, the Name is sys.indexes will be `'foo'`, not `'[foo]'`.
Remus Rusanu
If is still unclear: ` AND name = N'[IX_Sig_Summary1Index1_StartTime]'` should be `AND name = N'IX_Sig_Summary1Index1_StartTime'`...
Remus Rusanu