一个删除指定表的所有索引和统计的过程
网络编程 2021-07-05 16:33www.168986.cn编程入门
sql2005 一个删除指定表的所有索引和统计的过程
代码如下:
------------------------------------------------------------------------
-- Author : HappyFlyStone
-- Date : 2009-09-05 00:57:10
-- Version: Microsoft SQL Server 2005 - 9.00.2047.00 (Intel X86)
-- Apr 14 2006 01:12:25
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Enterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 2)
--
------------------------------------------------------------------------
IF EXISTS (SELECT name FROM sysobjects WHERE id = OBJECT_ID('sp_DropAllIndex')
AND OBJECTPROPERTY(OBJECT_ID('sp_DropAllIndex'),'IsProcedure')=1)
DROP PROCEDURE sp_DropAllIndex
GO
CREATE PROCEDURE sp_DropAllIndex
@tabname nvarchar(150) -- 需要删除统计或索引的表
AS
BEGIN
DECLARE @drop_idx_string nvarchar(4000) -- 存放动态组织而成的DROPS index/stats 语法
SET NOCOUNT ON
-- check table
IF NOT EXISTS (SELECT 1
FROM INFORMATION_SCHEMA.TABLES
WHERE table_type = 'base table' AND table_name = @tabname)
BEGIN
RAISERROR(N'------当前表''%s'' 不存在!',16, 1, @tabname)
RETURN (1)
END
SET @tabname = OBJECT_ID(@tabname)
IF EXISTS (SELECT 1
FROM sysindexes
WHERE id=@tabname AND indid BETWEEN 1 AND 254
AND status IN (96,10485856,8388704))
BEGIN
SELECT @drop_idx_string = isnull(@drop_idx_string+';','')
+ ('DROP STATISTICS '+OBJECT_NAME(@tabname)+'.'+name)
FROM sysindexes
WHERE id=@tabname AND indid BETWEEN 1 AND 254
AND status IN (96,10485856,8388704)
END
IF Len(@drop_idx_string) > 0
BEGIN
PRINT N'------统计删除列表------'
PRINT @drop_idx_string+';'
EXECUTE(@drop_idx_string+';')
PRINT N'------统计删除结束------'
END
IF EXISTS (SELECT 1 FROM sysindexes
WHERE id=@tabname AND indid BETWEEN 1 AND 254
AND status NOT IN (96,10485856,8388704))
BEGIN
SET @drop_idx_string = NULL
select @drop_idx_string = isnull(@drop_idx_string+';'+CHAR(13)+CHAR(10),'')
+ ('DROP INDEX '+OBJECT_NAME(@tabname)+'.'+name)
FROM sysindexes
WHERE id=@tabname AND indid BETWEEN 1 AND 254
AND status NOT IN (96,10485856,8388704)
AND OBJECTPROPERTY (OBJECT_ID(name),'IsConstraint') IS NULL--过程不处理CONSTRAINTS
END
PRINT N'------索引删除列表------'
PRINT (@drop_idx_string+';')
EXEC( @drop_idx_string+';')
PRINT ('......'+CHAR(13)+CHAR(10)+'......')
PRINT N'------索引删除结束------'
END
GO
create clustered index idx_id on ta(id)
create index idx_col on ta(col)
go
sp_DropAllIndex 'ta'
/
------索引删除列表------
DROP INDEX ta.idx_id;
DROP INDEX ta.idx_col;
......
......
------索引删除结束------
/
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程