--!!!!!!!!!!!!!!!!!!!!!
--!!!!!!!!慎用!!!!!!!!!
--!!!!!!!!!!!!!!!!!!!!!
--删除sql server中所有的表视图存储过程函数
--删除存储过程
DECLARE @STRING VARCHAR(8000)
WHILE EXISTS(SELECT NAME FROM SYSOBJECTS WHERE TYPE='P' AND STATUS>=0)
BEGIN
SELECT @STRING='DROP PROCEDURE '+NAME FROM SYSOBJECTS WHERE TYPE = 'P' AND STATUS>=0
--SELECT @STRING
EXEC(@STRING)
END
GO
--删除默认值或 DEFAULT 约束
DECLARE @STRING VARCHAR(8000)
WHILE EXISTS(SELECT NAME FROM SYSOBJECTS WHERE XTYPE='D')
BEGIN
SELECT @STRING='ALTER TABLE '+B.NAME+' DROP CONSTRAINT '+A.NAME
FROM (SELECT PARENT_OBJ,NAME FROM SYSOBJECTS WHERE XTYPE='D') A,
(SELECT ID,NAME FROM SYSOBJECTS WHERE OBJECTPROPERTY(ID, N'ISUSERTABLE') = 1) B
WHERE A.PARENT_OBJ=B.ID
EXEC(@STRING)
END
GO
--删除UNIQUE 约束
DECLARE @STRING VARCHAR(8000)
WHILE EXISTS(SELECT NAME FROM SYSOBJECTS WHERE XTYPE='UQ')
BEGIN
SELECT @STRING='ALTER TABLE '+B.NAME+' DROP CONSTRAINT '+A.NAME
FROM (SELECT PARENT_OBJ,NAME FROM SYSOBJECTS WHERE XTYPE='UQ') A,
(SELECT ID,NAME FROM SYSOBJECTS WHERE OBJECTPROPERTY(ID, N'ISUSERTABLE') = 1) B
WHERE A.PARENT_OBJ=B.ID
EXEC(@STRING)
END
GO
--删除FOREIGN KEY 约束
DECLARE @STRING VARCHAR(8000)
WHILE EXISTS(SELECT NAME FROM SYSOBJECTS WHERE TYPE='F')
BEGIN
SELECT @STRING='ALTER TABLE '+B.NAME+' DROP CONSTRAINT '+A.NAME
FROM (SELECT PARENT_OBJ,NAME FROM SYSOBJECTS WHERE TYPE='F') A,
(SELECT ID,NAME FROM SYSOBJECTS WHERE OBJECTPROPERTY(ID, N'ISUSERTABLE') = 1) B
WHERE A.PARENT_OBJ=B.ID
EXEC(@STRING)
END
GO
--删除PRIMARY KEY 约束
DECLARE @STRING VARCHAR(8000)
WHILE EXISTS(SELECT NAME FROM SYSOBJECTS WHERE XTYPE='PK')
BEGIN
SELECT @STRING='ALTER TABLE '+B.NAME+' DROP CONSTRAINT '+A.NAME
FROM (SELECT PARENT_OBJ,NAME FROM SYSOBJECTS WHERE XTYPE='PK') A,
(SELECT ID,NAME FROM SYSOBJECTS WHERE OBJECTPROPERTY(ID, N'ISUSERTABLE') = 1) B
WHERE A.PARENT_OBJ=B.ID
EXEC(@STRING)
END
GO
--删除触发器
DECLARE @STRING VARCHAR(8000)
WHILE EXISTS(SELECT NAME FROM SYSOBJECTS WHERE XTYPE='TR')
BEGIN
SELECT @STRING='DROP TRIGGER '+NAME FROM SYSOBJECTS WHERE XTYPE='TR'
EXEC(@STRING)
END
GO
--删除索引
declare @string varchar(8000)
while exists(
select TABLE_NAME= o.name,INDEX_NAME= x.name
fromsysobjects o, sysindexes x, syscolumns c, sysindexkeysxk
whereo.type in ('U')
and convert(bit,(x.status& 0x800)/0x800)=0
and x.id = o.id
and o.id = c.id
and o.id = xk.id
andx.indid = xk.indid
andc.colid = xk.colid
andxk.keyno<= x.keycnt
and permissions(o.id, c.name) <> 0
and (x.status&32) = 0 -- No hypothetical indexes
group by o.name,x.name)
begin
select top 1 @string='drop index '+o.name+'.'+ x.name
fromsysobjects o, sysindexes x, syscolumns c, sysindexkeysxk
whereo.type in ('U')
and convert(bit,(x.status& 0x800)/0x800)=0
and x.id = o.id
and o.id = c.id
and o.id = xk.id
andx.indid = xk.indid
andc.colid = xk.colid
andxk.keyno<= x.keycnt
and permissions(o.id, c.name) <> 0
and (x.status&32) = 0 -- No hypothetical indexes
group by o.name,x.name
exec(@string)
end
GO
--删除所有视图
declare @sql2 varchar(8000)
set @sql2= ''
select @sql2=@sql2+ ', '+name from sysobjects
where type= 'V ' and name not in( 'syssegments ', 'sysconstraints ')
set @sql2= 'drop view '+stuff(@sql2, 1, 1, ' ')
exec(@sql2)
GO
--删除所有表
--*****************************************************************************
-- @作者: qwu;
-- @用途: 删除数据库所有用户表;
-- @版本: v1.0
-- @创建日期: 2011-3-23;
-- @用法: 设置use [数据库名],
-- @注意: !!!!!!!!慎用!!!!!!!!!;
-- @修改历史:
-- 2011-3-23 qwu创建;
--*****************************************************************************
--!!!!!!!!!!!!!!!!!!!!!
--!!!!!!!!慎用!!!!!!!!!
--!!!!!!!!!!!!!!!!!!!!!
--BEGIN--
--BEGIN TRAN
--use [数据库名]
--删除外键约束
DECLARE c1 cursor for
select 'alter table ['+ object_name(parent_obj) + '] drop constraint ['+name+']; '
fromsysobjects
wherextype = 'F'
open c1
declare @c1 varchar(8000)
fetch next from c1 into @c1
while(@@fetch_status=0)
begin
exec(@c1)
fetch next from c1 into @c1
end
close c1
deallocate c1
--删除所有用户表
declare @sqlvarchar(8000)
while (select count(*) from sysobjects where type='U')>0
begin
SELECT @sql='drop table ' + name
FROM sysobjects
WHERE (type = 'U')
ORDER BY 'drop table ' + name
exec(@sql)
end
--ROLLBACK
--END--