Not ComplaintLog or even Complaint_Log
I want to drop an index, so enclosing it in square brackets does the trick
DROP INDEX
[IX_Complaint_Log_SNP_Number] ON [dbo].[Complaint Log];
And of course I could do it with the GUI, just by pointing and clicking, all that stuffBut, I want to do it with a script and run it at 0300, and I want to check whether the index actually exists before my script tries to drop it.
Normal thing would be to do something like this:
IF EXISTS(SELECT * FROM sys.indexes
WHERE object_id = object_id('dbo.Complaint Log')
AND NAME ='IX_Complaint_Log_SNP_Number')
DROP INDEX
[IX_Complaint_Log_SNP_Number] ON [dbo].[Complaint Log];
Command(s) completed successfully.
But it is lying - the index is not found, and it is not deleted
It would be very satisfying to hunt down the guy who created Complaints Log instead of Complaints_Log, but in these namby pamby days of political correctness gone mad, torturing and killing Developers is frowned upon.
Stefan Spraakman gave me the answer - thanks Stefan! I need to join sys.indexes with sys.objects and test on the name, like this:
IF EXISTS(SELECT * FROM sys.indexes AS i
INNER JOIN sys.objects AS o ON o.object_id = i.object_id
WHERE o.name = 'Complaint Log'
AND i.NAME ='IX_Complaint_Log_SNP_Number')
DROP INDEX [IX_Complaint_Log_SNP_Number] ON [dbo].[Complaint Log];
DROP INDEX [IX_Complaint_Log_SNP_Number] ON [dbo].[Complaint Log];
No comments:
Post a Comment