数据库中表内容的删除总结(总有你需要的)

来源:互联网 发布:湖南云计算 编辑:程序博客网 时间:2024/05/15 11:56
1.对数据库中的表批量清空删除 (慎用)
use PL123
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
EXEC sp_MSForEachTable 'ALTER TABLE ? DISABLE TRIGGER ALL'
EXEC sp_MSForEachTable 'DELETE from ?'
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
EXEC sp_MSForEachTable 'ALTER TABLE ? ENABLE TRIGGER ALL'
EXEC sp_MSFOREACHTABLE 'SELECT * from ?'
EXEC sp_MSFOREACHTABLE 'drop table ?'
GO
2.清空表

Truncate table tablename
delete from tablename
3.游标实现清空指定表 !!!游标经典应用
use TOMR_SYS_C_LP --选择你要清空的数据库
---禁用本库中所有表的外键约束
DECLARE Employee_Cursor CURSOR FOR
select name from sysobjects where xtype='u' and (not name LIKE 'dtproperties') order by [name] asc;
declare @tablename varchar(300);
declare @str varchar(1000);
declare @rst int;
OPEN Employee_Cursor;
FETCH NEXT FROM Employee_Cursor into @tablename;
WHILE @@FETCH_STATUS = 0
BEGIN
select @str = 'ALTER TABLE '+ @tablename + ' NOCHECK CONSTRAINT ALL';
EXECUTE(@str);
FETCH NEXT FROM Employee_Cursor into @tablename;
END
CLOSE Employee_Cursor;
DEALLOCATE Employee_Cursor;
GO

--删除表中数据
DECLARE delete_Cursor CURSOR FOR
select name from sysobjects where xtype='u' and (not name LIKE 'dtproperties') order by [name] asc;
declare @tablename varchar(300);
declare @str varchar(1000);
declare @rst int;
OPEN delete_Cursor;
FETCH NEXT FROM delete_Cursor into @tablename;
WHILE @@FETCH_STATUS = 0
BEGIN
--这里写你不需要删除数据的表名
IF(@tablename = 'xx')
begin
print('未删除表数据 '+@tablename)
end
ElSE
begin
select @str = 'delete from '+ @tablename;
EXECUTE(@str);
print('删除表数据 '+@tablename)
end
FETCH NEXT FROM delete_Cursor into @tablename;
END
CLOSE delete_Cursor;
DEALLOCATE delete_Cursor;
GO
--恢复所有表外键约束
DECLARE Employee_Cursor CURSOR FOR
select name from sysobjects where xtype='u' and (not name LIKE 'dtproperties') order by [name] asc;
declare @tablename varchar(300);
declare @str varchar(1000);
declare @rst int;
OPEN Employee_Cursor;
FETCH NEXT FROM Employee_Cursor into @tablename;
WHILE @@FETCH_STATUS = 0
BEGIN
select @str = 'ALTER TABLE '+ @tablename + ' CHECK CONSTRAINT ALL';
EXECUTE(@str);
FETCH NEXT FROM Employee_Cursor into @tablename;
END
CLOSE Employee_Cursor;
DEALLOCATE Employee_Cursor;
GO

实例:
use tyzh
--删除表数据readerdata
DECLARE delete_Cursor CURSOR FOR
select name from sysobjects where xtype='u' and (not name LIKE 'dtproperties')
and name like 'locatedata2011%'
or name like 'ReaderData2011%'
or name like 'stayInterval2011%'
order by [name] asc;
declare @tablename varchar(300);
declare @str varchar(1000);
declare @rst int;
OPEN delete_Cursor;
FETCH NEXT FROM delete_Cursor into @tablename;
WHILE @@FETCH_STATUS = 0
BEGIN
--这里写你不需要删除数据的表名
IF(
@tablename = 'locatedata20111221'
or
@tablename = 'ReaderData20111221'
or
@tablename = 'stayInterval20111221'
)
begin
print('-------------------------')
print('未删除表数据'+@tablename)
end
ElSE
begin
select @str = 'delete from '+ @tablename;
EXECUTE(@str);
print('删除表数据'+@tablename)
end
FETCH NEXT FROM delete_Cursor into @tablename;
END
CLOSE delete_Cursor;
DEALLOCATE delete_Cursor;
GO
系统存储批量清空SqlServer中所有表的数据
2010-12-19 14:03
清空SqlServer中所有表的数据
view sourceprint?1 sp_MSforeachtable @command1='Delete from ?'

view sourceprint?01 sp_MSforeachtable使用方法
02
03 1)说明
04 系统存储过程sp_MSforeachtable和sp_MSforeachdb,是微软提供的两个不公开的存储过程,从ms sql 6.5开始。
05 存放在SQL Server的MASTER数据库中。
06
07 2)参数说明:
08 @command1 nvarchar(2000), --第一条运行的SQL指令
09 @replacechar nchar(1) = N'?', --指定的占位符号
10 @command2 nvarchar(2000)= null, --第二条运行的SQL指令
11 @command3 nvarchar(2000)= null, --第三条运行的SQL指令
12 @whereand nvarchar(2000)= null, --可选条件来选择表
13 @precommand nvarchar(2000)= null, --执行指令前的操作(类似控件的触发前的操作)
14 @postcommand nvarchar(2000)= null --执行指令后的操作(类似控件的触发后的操作)
15
16 3)举例
17 --统计数据库里每个表的详细情况
18 exec sp_MSforeachtable @command1="sp_spaceused '?'"
19 --获得每个表的记录数和容量:
20 EXEC sp_MSforeachtable @command1="print '?'",
21 @command2="sp_spaceused '?'",
22 @command3= "SELECT count(*) FROM ? "
23 --获得所有的数据库的存储空间:
24 EXEC sp_MSforeachdb @command1="print '?'",
25 @command2="sp_spaceused "
26 --检查所有的数据库
27 EXEC sp_MSforeachdb @command1="print '?'",
28 @command2="DBCC CHECKDB (?) "
29 --更新PUBS数据库中已t开头的所有表的统计:
30 EXEC sp_MSforeachtable @whereand="and name like 't%'",
31 @replacechar='*',
32 @precommand="print 'Updating Statistics.....' print ''",
33 @command1="print '*' update statistics * ",
34 @postcommand= "print''print 'Complete Update Statistics!'"
35 --删除当前数据库所有表中的数据
36 sp_MSforeachtable @command1='Delete from ?'
37 sp_MSforeachtable @command1 = "TRUNCATE TABLE ?"
38
39 4)参数@whereand的用法
40 @whereand参数在存储过程中起到指令条件限制的作用,具体的写法如下:
41 @whereend,可以这么写 @whereand=' AND o.name in (''Table1'',''Table2'',.......)'
42 例如:我想更新Table1/Table2/Table3中NOTE列为NULL的值
43 sp_MSforeachtable @command1='Update ? Set NOTE='''' Where NOTE is NULL',@whereand=' AND o.name in (''Table1'',''Table2'',''Table3'')'
44
45 5)"?"在存储过程的特殊用法,造就了这两个功能强大的存储过程
46 这里"?"的作用,相当于DOS命令中、以及我们在WINDOWS下搜索文件时的通配符的作用。


print '打印所有需要清空的表:'
exec sp_msforeachtable @command1="print '?'",
@whereand=' and (o.[name] like ''YDS_%'' or o.[name] like ''FKS_%'')'
print '---------------------------------------------------------------------'
print '打印没有外键约束的表:'
exec sp_msforeachtable @command1="print '?'",
@whereand=' and objectproperty(o.id,''TableHasForeignRef'')=0 and xtype=''u'' and (o.[name] like ''YDS_%'' or o.[name] like ''SGCB_%'' or o.[name] like ''YDZY_%'' or o.[name] like ''Document_%'')'
--清空没有外键约束的表
exec sp_msforeachtable @command1="truncate table ?",
@whereand=' and objectproperty(o.id,''TableHasForeignRef'')=0 and xtype=''u'' and (o.[name] like ''YDS_%'' or o.[name] like ''SGCB_%'' or o.[name] like ''YDZY_%'' or o.[name] like ''Document_%'')'
print '---------------------------------------------------------------------'
truncate table CHZL_XX
truncate table BookMarks
print '打印有外键约束的表:'
exec sp_msforeachtable @command1="print '?'",
@whereand=' and objectproperty(o.id,''TableHasForeignRef'')=1 and xtype=''u'' and (o.[name] like ''YDS_%'' or o.[name] like ''SGCB_%'' or o.[name] like ''YDZY_%'' or o.[name] like ''Document_%'')'
--清空有外键约束的表
exec sp_msforeachtable @command1="delete from ?",
@whereand=' and objectproperty(o.id,''TableHasForeignRef'')=1 and xtype=''u'' and (o.[name] like ''YDS_%'' or o.[name] like ''SGCB_%'' or o.[name] like ''YDZY_%'' or o.[name] like ''Document_%'')'
print '---------------------------------------------------------------------'

print '重置MaxIDs表:'
--重置MaxIDs表

update MaxIDs set MaxID=1


文章参考地址:http://blog.sina.com.cn/s/blog_790229d301016aii.html