方法一:
--生成数据库脚本的方法最快,处理的也最好
sql2000企业管理器
--右键要清理的数据库
--所有任务
--生成SQL脚本
--<常规>里选择"生成全部对象脚本"","在脚本文件中包含说明性标题"选上
--<设置格式>里,将"包含扩展属性",选上
--<选项>中,将"表脚本选项"中的内容全部选择上
--<选项>中,全选
--其他所有的选项保持默认值
--然后确定,将其保存成一个.sql文件
然后断开对对要清空数据库的连接,查询分析器中执行上面的sql文件就可以了.
方法二:
1--Use 数据库名称
2use databasename
3--第一部分,生成建立外键的语句保存到#tmp
4declare @name varchar(200),@tmp1 varchar(500),@tmp2 varchar(500)
5
6create table #tmp
7(
8string varchar(8000)
9)
10
11SELECT 表名称=object_name(b.fkeyid)
12 ,外键名称=a.name
13 ,引用的列名=(SELECT name FROM syscolumns WHERE colid=b.fkey AND id=b.fkeyid)
14 ,引用的表名=object_name(b.rkeyid)
15 ,已引用的列名=(SELECT name FROM syscolumns WHERE colid=b.rkey AND id=b.rkeyid)
16into #t FROM sysobjects a
17 join sysforeignkeys b on a.id=b.constid
18 join sysobjects c on a.parent_obj=c.id
19where a.xtype='f' AND c.xtype='U'
20
21declare cur_test cursor for
22 select a.name from sysobjects a join sysobjects c on a.parent_obj=c.id where a.xtype='f' and c.xtype='U'
23open cur_test
24FETCH NEXT FROM cur_test INTO @name
25WHILE (@@fetch_status <> -1)
26BEGIN
27 IF (@@fetch_status <> -2)
28 BEGIN
29 select @tmp1='',@tmp2=''
30 select @tmp1=@tmp1+'['+引用的列名+'],',@tmp2=@tmp2+'['+已引用的列名+'],' from #t where 外键名称=@name
31 insert into #tmp select top 1 'ALTER TABLE [DBO].['+表名称+'] ADD CONSTRAINT ['+@name+'] FOREIGN KEY ( '+left(@tmp1,len(@tmp1)-1)+' ) REFERENCES ['+引用的表名+'] ( '+left(@tmp2,len(@tmp2)-1)+' )' from #t where 外键名称=@name
32 END
33 FETCH NEXT FROM cur_test INTO @name
34END
35
36CLOSE cur_test
37DEALLOCATE cur_test
38drop table #t
39
40--第二部分,删除所有外键
41DECLARE @STRING VARCHAR(8000)
42WHILE EXISTS(SELECT NAME FROM SYSOBJECTS WHERE TYPE='F')
43BEGIN
44 SELECT @STRING='ALTER TABLE '+B.NAME+' DROP CONSTRAINT '+A.NAME+CHAR(13)
45 FROM (SELECT PARENT_OBJ,NAME FROM SYSOBJECTS WHERE TYPE='F') A,
46 (SELECT ID,NAME FROM SYSOBJECTS WHERE OBJECTPROPERTY(ID, N'ISUSERTABLE') = 1) B
47 WHERE A.PARENT_OBJ=B.ID
48 EXEC(@STRING)
49END
50
51--第三部分,删除所有表的记录,并且把identity复位
52exec sp_msforeachtable "truncate table ?"
53
54--第四部分,执行#tmp里面的建立外键的语句,恢复外键
55declare cur_test2 cursor for select string from #tmp
56
57open cur_test2
58FETCH NEXT FROM cur_test2 INTO @string
59WHILE (@@fetch_status <> -1)
60BEGIN
61 IF (@@fetch_status <> -2)
62 BEGIN
63 exec(@string)
64 PRINT @STRING
65 END
66 FETCH NEXT FROM cur_test2 INTO @string
67END
68
69CLOSE cur_test2
70DEALLOCATE cur_test2
71
72drop table #tmp
73
74
2use databasename
3--第一部分,生成建立外键的语句保存到#tmp
4declare @name varchar(200),@tmp1 varchar(500),@tmp2 varchar(500)
5
6create table #tmp
7(
8string varchar(8000)
9)
10
11SELECT 表名称=object_name(b.fkeyid)
12 ,外键名称=a.name
13 ,引用的列名=(SELECT name FROM syscolumns WHERE colid=b.fkey AND id=b.fkeyid)
14 ,引用的表名=object_name(b.rkeyid)
15 ,已引用的列名=(SELECT name FROM syscolumns WHERE colid=b.rkey AND id=b.rkeyid)
16into #t FROM sysobjects a
17 join sysforeignkeys b on a.id=b.constid
18 join sysobjects c on a.parent_obj=c.id
19where a.xtype='f' AND c.xtype='U'
20
21declare cur_test cursor for
22 select a.name from sysobjects a join sysobjects c on a.parent_obj=c.id where a.xtype='f' and c.xtype='U'
23open cur_test
24FETCH NEXT FROM cur_test INTO @name
25WHILE (@@fetch_status <> -1)
26BEGIN
27 IF (@@fetch_status <> -2)
28 BEGIN
29 select @tmp1='',@tmp2=''
30 select @tmp1=@tmp1+'['+引用的列名+'],',@tmp2=@tmp2+'['+已引用的列名+'],' from #t where 外键名称=@name
31 insert into #tmp select top 1 'ALTER TABLE [DBO].['+表名称+'] ADD CONSTRAINT ['+@name+'] FOREIGN KEY ( '+left(@tmp1,len(@tmp1)-1)+' ) REFERENCES ['+引用的表名+'] ( '+left(@tmp2,len(@tmp2)-1)+' )' from #t where 外键名称=@name
32 END
33 FETCH NEXT FROM cur_test INTO @name
34END
35
36CLOSE cur_test
37DEALLOCATE cur_test
38drop table #t
39
40--第二部分,删除所有外键
41DECLARE @STRING VARCHAR(8000)
42WHILE EXISTS(SELECT NAME FROM SYSOBJECTS WHERE TYPE='F')
43BEGIN
44 SELECT @STRING='ALTER TABLE '+B.NAME+' DROP CONSTRAINT '+A.NAME+CHAR(13)
45 FROM (SELECT PARENT_OBJ,NAME FROM SYSOBJECTS WHERE TYPE='F') A,
46 (SELECT ID,NAME FROM SYSOBJECTS WHERE OBJECTPROPERTY(ID, N'ISUSERTABLE') = 1) B
47 WHERE A.PARENT_OBJ=B.ID
48 EXEC(@STRING)
49END
50
51--第三部分,删除所有表的记录,并且把identity复位
52exec sp_msforeachtable "truncate table ?"
53
54--第四部分,执行#tmp里面的建立外键的语句,恢复外键
55declare cur_test2 cursor for select string from #tmp
56
57open cur_test2
58FETCH NEXT FROM cur_test2 INTO @string
59WHILE (@@fetch_status <> -1)
60BEGIN
61 IF (@@fetch_status <> -2)
62 BEGIN
63 exec(@string)
64 PRINT @STRING
65 END
66 FETCH NEXT FROM cur_test2 INTO @string
67END
68
69CLOSE cur_test2
70DEALLOCATE cur_test2
71
72drop table #tmp
73
74
from: http://topic.csdn.net/t/20040615/16/3094555.html#