-- ============================================= -- 标题:禁用聚集索引来阻止对表的访问(暂停使用某个表,但不删除) -- 作者:dobear -- 环境:SQL2005 -- 日期:2008-04-01 -- ============================================= use Testdb --选择数据库,请使用服务器上已有的数据库 ifobject_id(N'tb', 'U') isnotnull droptable tb go -- 1 创建测试表,添加测试数据 createtable tb ( id intprimarykey, name nvarchar(32) ) insert tb select1, '1111' insert tb select3, '3333' insert tb select2, '2222' --禁用前 select*from tb --正常,能查到数据 /**//* id name ----------- -------------------------------- 1 1111 2 2222 3 3333 (3 row(s) affected) */ --找到聚集索引(一般主键上会默认创建一个聚集索引),然后禁用 declare@sqlnvarchar(max), @index sysname select@index=name from sys.indexes whereobject_id=object_id(N'tb') and type=1 set@sql='alter index '+@index+' on tb Disable' exec(@sql) --禁用后 select*from tb --报错,表不可用 /**//* Msg 8655, Level 16, State 1, Line 22 The query processor is unable to produce a plan because the index 'PK__tb__1BC821DD' on table or view 'tb' is disabled. */