Making changes to an existing table – in this case NULLability – with indexes on the table and you may get the following error:

Msg 5074, Level 16, State 1, Line 2
The index 'idx_test' is dependent on column 'isDeleted'.
Msg 4922, Level 16, State 9, Line 2
ALTER TABLE ALTER COLUMN isDeleted failed because one or more objects access this column.

 

In this instance, you’ll have to drop the index(es) referencing the column before you can alter the column. If you try to disable the index via ALTER INDEX indexname on table DISABLE, you’ll get the same error…no can do. Have to get rid of it first. Here’s an example:

DROP TABLE idxTest
GO
CREATE TABLE idxTest
    (ID INT
    ,isDeleted BIT NULL
    )
GO
INSERT idxTest VALUES (1,0)
GO
CREATE INDEX idx_test ON idxTest (ID, isdeleted)
GO
 
--ALTER TABLE idxTest
-- ALTER COLUMN isDeleted BIT NOT null
GO
 
DROP INDEX idxTest.idx_test
GO
ALTER TABLE idxTest
    ALTER COLUMN isDeleted BIT NOT null
GO
CREATE INDEX idx_test ON idxTest (ID, isdeleted)
GO

 

Notice that I’ve commented out the ALTER TABLE, but you can uncomment and give it a try.

Thanks for reading,
Lee

 

----------------------

 

 


Posted in: Beginner , SQL Administration , TSQL  Tags:
blog comments powered by Disqus

by Lee Everest, M.S.

Poll

Do you use Azure or cloud in your organization?



Show Results

Ads

Search


Month List

Calendar

«  February 2012  »
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011
View posts in large calendar

Tags

Disclaimer
The opinions, code, examples, et.al. expressed herein are my own personal opinions and do not represent my employer's view in any way, shape form, or fashion.  All code for demonstration purposes - no guarantees, either written or implied, are made.

© Copyright 2012 Lee Everest's SQL Server, etc. weblog