`

【推荐】(SqlServer)不公开存储过程sp_Msforeachtable与sp_Msforeachdb详解

 
阅读更多

推荐(SqlServer)不公开存储过程


sp_Msforeachtablesp_Msforeachdb详解


——通过知识共享树立个人品牌。

一.简要介绍:

系统存储过程sp_MSforeachtablesp_MSforeachdb,是微软提供的两个不公开的存储过程。从mssql6.5开始,存放在SQL Server的MASTER数据库中。可以用来对某个数据库的所有表或某个SQL服务器上的所有数据库进行管理,下面将对此进行详细介绍。

作为数据库管理者或开发者等经常会检查整个数据库或用户表。

如:检查整个数据库的容量,看指定数据库所有用户表的容量,所有表的记录数等等,我们一般处理这样的问题都是通过游标来达到要求。

如果我们用sp_MSforeachtable就可以非常方便的达到相同的目的,

如:sql查询所有用户表的列表,详细信息,如:记录数,表占用大小等

EXECsp_MSforeachtable"EXECUTEsp_spaceused'?'"

二.各参数说明:

@command1nvarchar(2000),--第一条运行的SQL指令
@replacecharnchar(1)=N'?',--指定的占位符号
@command2nvarchar(2000)=null,--第二条运行的SQL指令
@command3nvarchar(2000)=null,--第三条运行的SQL指令
@whereandnvarchar(2000)=null,--可选条件来选择表
@precommandnvarchar(2000)=null,--执行指令前的操作(类似控件的触发前的操作)
@postcommandnvarchar(2000)=null--执行指令后的操作(类似控件的触发后的操作)

以后为sp_MSforeachtable的参数,sp_MSforeachdb不包括参数@whereand

我们在master数据库里执行下面的语句可以看到两个proc详细的代码

usemaster
execsp_helptextsp_MSforeachtable
execsp_helptextsp_Msforeachdb

三、使用举例:

--统计数据库里每个表的详细情况:

execsp_MSforeachtable@command1="sp_spaceused'?'"

--获得每个表的记录数和容量:
EXECsp_MSforeachtable@command1="print'?'",
@command2="sp_spaceused'?'",
@command3="SELECTcount(*)FROM?"

--获得所有的数据库的存储空间:
EXECsp_MSforeachdb@command1="print'?'",
@command2="sp_spaceused"

--检查所有的数据库
EXECsp_MSforeachdb@command1="print'?'",
@command2="DBCCCHECKDB(?)"

--更新PUBS数据库中已t开头的所有表的统计:
EXECsp_MSforeachtable@whereand="andnamelike't%'",
@replacechar='*',
@precommand="print'UpdatingStatistics.....'print''",
@command1="print'*'updatestatistics*",
@postcommand="print''print'CompleteUpdateStatistics!'"

--删除当前数据库所有表中的数据
sp_MSforeachtable@command1='Deletefrom?'
sp_MSforeachtable@command1="TRUNCATETABLE?"

--查询数据库所有表的记录总数
CREATETABLE#temp(TableNameVARCHAR(255),RowCntINT)
EXECsp_MSforeachtable'INSERTINTO#tempSELECT''?'',COUNT(*)FROM?'
SELECTTableName,RowCntFROM#tempORDERBYTableName
DROPTABLE#temp

--检查数据库里每个表或索引视图的数据、索引及text、ntext和image页的完整性
--
下列语句需在单用户模式下执行(sp_dboption'db_name','singleuser','true')
--
,将true改成false就又变成多用户了
execsp_msforeachtable"dbccchecktable('?',repair_rebuild)"

4.参数@whereand的用法:

@whereand参数在存储过程中起到指令条件限制的作用,具体的写法如下:

@whereend,可以这么写

@whereand='ANDo.namein(''Table1'',''Table2'',.......)'

又如:

我想更新Table1/Table2/Table3中NOTE列为NULL的值

sp_MSforeachtable@command1='Update?SetNOTE=''''WhereNOTEisNULL'
,@whereand='ANDo.namein(''Table1'',''Table2'',''Table3'')'

5. "?"特别说明:

"?"在存储过程的特殊用法,造就了这两个功能强大的存储过程.

"?"的作用,相当于DOS命令中和我们在WINDOWS下搜索文件时的通配符的作用。

6.小结

有上面的分析,我们可以建立自己的sp_MSforeachObject:(注:下面的内容来源于网上。

USEMASTER

GO
CREATEprocsp_MSforeachObject
@objectTypeint=1,
@command1nvarchar(2000),
@replacecharnchar(1)=N'?',
@command2nvarchar(2000)=null,
@command3nvarchar(2000)=null,
@whereandnvarchar(2000)=null,
@precommandnvarchar(2000)=null,
@postcommandnvarchar(2000)=null
as
/*Thisprocreturnsoneormorerowsforeachtable(optionally,matching@where),witheachtabledefaultingtoits
ownresultset
*/
/*@precommandand@postcommandmaybeusedtoforceasingleresultsetviaatemptable.*/
/*Preprocessorwon'treplacewithinquotessohavetousestr().*/
declare@mscatnvarchar(12)
select@mscat=ltrim(str(convert(int,0x0002)))
if(@precommandisnotnull)
exec(@precommand)
/*Defined@isobjectforsaveobjecttype*/
Declare@isobjectvarchar(256)
select@isobject=case@objectTypewhen1then'IsUserTable'
when2then'IsView'
when3then'IsTrigger'
when4then'IsProcedure'
when5then'IsDefault'
when6then'IsForeignKey'
when7then'IsScalarFunction'
when8then'IsInlineFunction'
when9then'IsPrimaryKey'
when10then'IsExtendedProc'
when11then'IsReplProc'
when12then'IsRule'
end
/*Createtheselect*/
/*Use@isobjectvariableissteadofIsUserTablestring*/
EXEC(N'declarehCForEachcursorglobalforselect''[''+REPLACE(user_name(uid),N'']'',N'']]'')+'']''+''.''+''[''+
REPLACE(object_name(id),N
'']'',N'']]'')+'']''fromdbo.sysobjectso'
+N'whereOBJECTPROPERTY(o.id,N'''+@isobject+''')=1'+N'ando.category&'+@mscat+N'=0'
+@whereand)
declare@retvalint
select@retval=@@error
if(@retval=0)
exec@retval=sp_MSforeach_worker@command1,@replacechar,@command2,@command3
if(@retval=0and@postcommandisnotnull)
exec(@postcommand)
return@retval
GO

我们来测试一下:

--获得所有的存储过程的脚本:
EXEcsp_MSforeachObject@command1="sp_helptext'?'",@objectType=4

--获得所有的视图的脚本:
EXEcsp_MSforeachObject@command1="sp_helptext'?'",@objectType=2

--比如在开发过程中,没一个用户都是自己的OBJECTOWNER,所以在真实的数据库时都要改为DBO:
EXEcsp_MSforeachObject@command1="sp_changeobjectowner'?','dbo'",@objectType=1
EXEcsp_MSforeachObject@command1="sp_changeobjectowner'?','dbo'",@objectType=2
EXEcsp_MSforeachObject@command1="sp_changeobjectowner'?','dbo'",@objectType=3
EXEcsp_MSforeachObject@command1="sp_changeobjectowner'?','dbo'",@objectType=4

© 2011EricHu

原创作品,转贴请注明作者和出处,留此信息。

------------------------------------------------

cnBlobs:http://www.cnblogs.com/huyong/
CSDNhttp://blog.csdn.net/chinahuyong

作者:EricHuDBC\SB\SWebServiceWCFPM等)
出处:http://www.cnblogs.com/huyong/

Q Q80368704 E-Mail: 80368704@qq.com
本博文欢迎大家浏览和转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,在『参考』的文章中,我会表明参考的文章来源,尊重他人版权。若您发现我侵犯了您的版权,请及时与我联系。
更多文章请看[置顶]索引贴——(不断更新中)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics