--
=============================================
-- 标题:禁用聚集索引来阻止对表的访问(暂停使用某个表,但不删除)
-- 作者:dobear
-- 环境:SQL2005
-- 日期:2008-04-01
-- =============================================
use Testdb -- 选择数据库,请使用服务器上已有的数据库
if object_id (N ' tb ' , ' U ' ) is not null
drop table tb
go
-- 1 创建测试表,添加测试数据
create table tb
(
id int primary key ,
name nvarchar ( 32 )
)
insert tb select 1 , ' 1111 '
insert tb select 3 , ' 3333 '
insert tb select 2 , ' 2222 '
-- 禁用前
select * from tb -- 正常,能查到数据
/**/ /*
id name
----------- --------------------------------
1 1111
2 2222
3 3333
(3 row(s) affected)
*/
-- 找到聚集索引(一般主键上会默认创建一个聚集索引),然后禁用
declare @sql nvarchar ( max ), @index sysname
select @index = name from sys.indexes where object_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.
*/
-- 标题:禁用聚集索引来阻止对表的访问(暂停使用某个表,但不删除)
-- 作者:dobear
-- 环境:SQL2005
-- 日期:2008-04-01
-- =============================================
use Testdb -- 选择数据库,请使用服务器上已有的数据库
if object_id (N ' tb ' , ' U ' ) is not null
drop table tb
go
-- 1 创建测试表,添加测试数据
create table tb
(
id int primary key ,
name nvarchar ( 32 )
)
insert tb select 1 , ' 1111 '
insert tb select 3 , ' 3333 '
insert tb select 2 , ' 2222 '
-- 禁用前
select * from tb -- 正常,能查到数据
/**/ /*
id name
----------- --------------------------------
1 1111
2 2222
3 3333
(3 row(s) affected)
*/
-- 找到聚集索引(一般主键上会默认创建一个聚集索引),然后禁用
declare @sql nvarchar ( max ), @index sysname
select @index = name from sys.indexes where object_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.
*/