您好,欢迎访问一九零五行业门户网

清除一个数据库里所有表的数据

如果需要清除一个数据库里所有表的数据,则可用下面命令: exec sp_msforeachtable truncate table ? ************************************************************************ 关于sp_msforeachtable和sp_msforeachdb 1)说明 系统存储过程sp_msforeachta
如果需要清除一个数据库里所有表的数据,则可用下面命令:
exec sp_msforeachtable truncate table ?
************************************************************************
关于sp_msforeachtable和sp_msforeachdb
1)说明
系统存储过程sp_msforeachtable和sp_msforeachdb,是微软提供的两个不公开的存储过程,从ms sql 6.5开始。
存放在sql server的master数据库中。
2)参数说明:
@command1 nvarchar(2000),          --第一条运行的sql指令
@replacechar nchar(1) = n'?',      --指定的占位符号
@command2 nvarchar(2000)= null,    --第二条运行的sql指令
@command3 nvarchar(2000)= null,    --第三条运行的sql指令
@whereand nvarchar(2000)= null,    --可选条件来选择表
@precommand nvarchar(2000)= null,  --执行指令前的操作(类似控件的触发前的操作)
@postcommand nvarchar(2000)= null  --执行指令后的操作(类似控件的触发后的操作)
3)举例
--统计数据库里每个表的详细情况
exec sp_msforeachtable @command1=sp_spaceused '?'
--获得每个表的记录数和容量:
exec sp_msforeachtable @command1=print '?',
@command2=sp_spaceused '?',
@command3= select count(*) from ?
--获得所有的数据库的存储空间:
exec sp_msforeachdb   @command1=print '?',
@command2=sp_spaceused
--检查所有的数据库
exec sp_msforeachdb   @command1=print '?',
@command2=dbcc checkdb (?)
--更新pubs数据库中已t开头的所有表的统计:
exec sp_msforeachtable @whereand=and name like 't%',
@replacechar='*',
@precommand=print 'updating statistics.....' print '',
@command1=print '*' update statistics * ,
@postcommand= print''print 'complete update statistics!'
--删除当前数据库所有表中的数据
sp_msforeachtable @command1='delete from ?'
exec sp_msforeachtable truncate table ?
4)参数@whereand的用法
@whereand参数在存储过程中起到指令条件限制的作用,具体的写法如下:
@whereend,可以这么写 @whereand=' and o.name in (''table1'',''table2'',.......)'
例如:我想更新table1/table2/table3中note列为null的值
sp_msforeachtable @command1='update ? set note='''' where note is null',@whereand=' and o.name in (''table1'',''table2'',''table3'')'
5)?在存储过程的特殊用法,造就了这两个功能强大的存储过程
这里?的作用,相当于dos命令中、以及我们在windows下搜索文件时的通配符的作用。
其它类似信息

推荐信息