水滴石穿

渴望成为高手--Amy.Qiu
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

SQL删除数据库所有Index,constraint

Posted on 2008-05-08 10:29  AmyQiu  阅读(548)  评论(0编辑  收藏  举报

declare @dropSQL nvarchar(max)
set @dropSQL=N''
select distinct @dropSQL=@dropSQL+'ALTER TABLE dbo.'+table_name+' DROP CONSTRAINT '+constraint_name+';'
from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE

EXEC dbo.sp_executesql @dropSQL
GO
declare @dropSQL nvarchar(max)

set @dropSQL=N''

select distinct @dropSQL=@dropSQL+'DROP INDEX '+name+' ON dbo.'+object_name(object_id)+';'
from sys.indexes
where name is not null and object_name(object_id)
in (select distinct table_name from LIVEDB.information_schema.columns 
 )
EXEC dbo.sp_executesql @dropSQL