系统存储过程sp_MSforeachtable和sp_MSforeachdb使用说明

1.简介:
作为dba会经常需要检查所有的数据库或用户表,比如:检查所有数据库的容量;看看指定数据库所有用户表的容量,所有表的记录数…,我们一般处理这样的问题都是用游标分别处理处理,比如:在数据库检索效率非常慢时,我们想检查数据库所有的用户表,我们就必须通过写游标来达到要求;如果我们用sp_msforeachtable就可以非常方便的达到相同的目的:exec sp_msforeachtable @command1=”print ‘?’ dbcc checktable (‘?’)”
系统存储过程sp_msforeachtable和sp_msforeachdb,是微软提供的两个不公开的存储过程,从mssql6.5开始。存放在sql server的master数据库中。可以用来对某个数据库的所有表或某个sql服务器上的所有数据库进行管理,后面将对此进行详细介绍。

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 –执行指令后的操作(类似控件的触发后的操作)

以后为sp_msforeachtable的参数,sp_msforeachdb不包括参数@whereand

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 ?’
sp_msforeachtable @command1 = “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下搜索文件时的通配符的作用。

6.小结

有了上面的分析,我们可以建立自己的sp_msforeachobject:(转贴)
use master
go
create proc sp_msforeachobject
@objecttype int=1,
@command1 nvarchar(2000),
@replacechar nchar(1) = n’?’,
@command2 nvarchar(2000) = null,
@command3 nvarchar(2000) = null,
@whereand nvarchar(2000) = null,
@precommand nvarchar(2000) = null,
@postcommand nvarchar(2000) = null
as
/* this proc returns one or more rows for each table (optionally, matching @where), with each table defaulting to its
own result set */
/* @precommand and @postcommand may be used to force a single result set via a temp table. */
/* preprocessor won’t replace within quotes so have to use str(). */
declare @mscat nvarchar(12)
select @mscat = ltrim(str(convert(int, 0x0002)))
if (@precommand is not null)
exec(@precommand)
/* defined @isobject for save object type */
declare @isobject varchar(256)
select @isobject= case @objecttype when 1 then ‘isusertable’
when 2 then ‘isview’
when 3 then ‘istrigger’
when 4 then ‘isprocedure’
when 5 then ‘isdefault’
when 6 then ‘isforeignkey’
when 7 then ‘isscalarfunction’
when 8 then ‘isinlinefunction’
when 9 then ‘isprimarykey’
when 10 then ‘isextendedproc’
when 11 then ‘isreplproc’
when 12 then ‘isrule’
end
/* create the select */
/* use @isobject variable isstead of isusertable string */
exec(n’declare hcforeach cursor global for select ”[” + replace(user_name(uid), n”]”, n”]]”) + ”]” + ”.” + ”[” +
replace(object_name(id), n”]”, n”]]”) + ”]” from dbo.sysobjects o ‘
+ n’ where objectproperty(o.id, n”’+@isobject+”’) = 1 ‘+n’ and o.category & ‘ + @mscat + n’ = 0 ‘
+ @whereand)
declare @retval int
select @retval = @@error
if (@retval = 0)
exec @retval = sp_msforeach_worker @command1, @replacechar, @command2, @command3
if (@retval = 0 and @postcommand is not null)
exec(@postcommand)
return @retval
go

这样我们来测试一下:
–获得所有的存储过程的脚本:
exec sp_msforeachobject @command1=”sp_helptext ‘?’ “,@objecttype=4
–获得所有的视图的脚本:
exec sp_msforeachobject @command1=”sp_helptext ‘?’ “,@objecttype=2
–比如在开发过程中,没一个用户都是自己的object owner,所以在真实的数据库时都要改为dbo:
exec sp_msforeachobject @command1=”sp_changeobjectowner ‘?’, ‘dbo'”,@objecttype=1
exec sp_msforeachobject @command1=”sp_changeobjectowner ‘?’, ‘dbo'”,@objecttype=2
exec sp_msforeachobject @command1=”sp_changeobjectowner ‘?’, ‘dbo'”,@objecttype=3
exec sp_msforeachobject @command1=”sp_changeobjectowner ‘?’, ‘dbo'”,@objecttype=4
这样就非常方便的将每一个数据库对象改为dbo.

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐