禁用聚集索引来阻止对表的访问(暂停使用某个表,但不删除)

本文介绍了一种通过禁用SQL Server 2005中表的聚集索引来临时阻止对该表访问的方法。这种方法可以在不需要删除表的情况下实现对表的暂停使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

-- =============================================
--
 标题:禁用聚集索引来阻止对表的访问(暂停使用某个表,但不删除)
--
 作者: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.
*/
 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值