存储过程:sp_MSforeachtable/sp_MSforeachdb

数据库 SQL Server
本文我们主要介绍了SQL Server的两个存储过程:sp_MSforeachtable/sp_MSforeachdb的参数说明以及使用示例,希望能够对您有所帮助。

SQL Server数据库的两个存储过程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 '?'"

--获得每个表的记录数和容量:

  1. EXEC sp_MSforeachtable @command1="print '?'",  
  2. @command2="sp_spaceused '?'",  
  3. @command3"SELECT count(*) FROM ? " 

--获得所有的数据库的存储空间:

  1. EXEC sp_MSforeachdb @command1="print '?'",  
  2. @command2="sp_spaceused " 

--检查所有的数据库

  1. EXEC sp_MSforeachdb @command1="print '?'",  
  2. @command2="DBCC CHECKDB (?) " 

--更新PUBS数据库中已t开头的所有表的统计:

  1. EXEC sp_MSforeachtable @whereand="and name like 't%'",  
  2. @replacechar='*',  
  3. @precommand="print 'Updating Statistics.....' print ''",  
  4. @command1="print '*' update statistics * ",  
  5. @postcommand"print''print 'Complete Update Statistics!'" 

--删除当前数据库所有表中的数据

  1. sp_MSforeachtable @command1='Delete from ?' 
  2. 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:(转贴)

  1. USE MASTER  
  2. GO  
  3. CREATE proc sp_MSforeachObject  
  4. @objectType int=1,  
  5. @command1 nvarchar(2000),   
  6. @replacechar nchar(1) = N'?',   
  7. @command2 nvarchar(2000) = null,  
  8. @command3 nvarchar(2000) = null,   
  9. @whereand nvarchar(2000) = null,  
  10. @precommand nvarchar(2000) = null,   
  11. @postcommand nvarchar(2000) = null  
  12. as  
  13. /* This proc returns one or more rows for each table (optionally, matching @where), with each table defaulting to its   
  14. own result set */  
  15. /* @precommand and @postcommand may be used to force a single result set via a temp table. */  
  16. /* Preprocessor won't replace within quotes so have to use str(). */  
  17. declare @mscat nvarchar(12)  
  18. select @mscat = ltrim(str(convert(int, 0x0002)))  
  19. if (@precommand is not null)  
  20. exec(@precommand)  
  21. /* Defined @isobject for save object type */  
  22. Declare @isobject varchar(256)  
  23. select @isobjectcase @objectType when 1 then 'IsUserTable'  
  24. when 2 then 'IsView'  
  25. when 3 then 'IsTrigger'  
  26. when 4 then 'IsProcedure'   
  27. when 5 then 'IsDefault'   
  28. when 6 then 'IsForeignKey'  
  29. when 7 then 'IsScalarFunction'  
  30. when 8 then 'IsInlineFunction'  
  31. when 9 then 'IsPrimaryKey'  
  32. when 10 then 'IsExtendedProc'   
  33. when 11 then 'IsReplProc'  
  34. when 12 then 'IsRule'  
  35. end  
  36. /* Create the select */  
  37. /* Use @isobject variable isstead of IsUserTable string */  
  38. EXEC(N'declare hCForEach cursor global for select ''['' + REPLACE(user_name(uid), N'']'', N'']]'') + '']'' + ''.'' + ''['' +   
  39. REPLACE(object_name(id), N'']'', N'']]'') + '']'' from dbo.sysobjects o '  
  40. + N' where OBJECTPROPERTY(o.id, N'''+@isobject+''') = 1 '+N' and o.category & ' + @mscat + N' = 0 '  
  41. + @whereand)  
  42. declare @retval int  
  43. select @retval = @@error  
  44. if (@retval = 0)  
  45. exec @retval = sp_MSforeach_worker @command1, @replacechar, @command2, @command3  
  46. if (@retval = 0 and @postcommand is not null)  
  47. exec(@postcommand)  
  48. return @retval  
  49. GO 

这样我们来测试一下:
--获得所有的存储过程的脚本:

  1. EXEc sp_MSforeachObject @command1="sp_helptext '?' ",@objectType=4 

--获得所有的视图的脚本:

  1. EXEc sp_MSforeachObject @command1="sp_helptext '?' ",@objectType=2 

--比如在开发过程中,没一个用户都是自己的OBJECT OWNER,所以在真实的数据库时都要改为DBO:

  1. EXEc sp_MSforeachObject @command1="sp_changeobjectowner '?', 'dbo'",@objectType=1 
  2. EXEc sp_MSforeachObject @command1="sp_changeobjectowner '?', 'dbo'",@objectType=2 
  3. EXEc sp_MSforeachObject @command1="sp_changeobjectowner '?', 'dbo'",@objectType=3 
  4. EXEc sp_MSforeachObject @command1="sp_changeobjectowner '?', 'dbo'",@objectType=4 

这样就非常方便的将每一个数据库对象改为DBO。

关于SQL Server数据库的两个存储过程:sp_MSforeachtable/sp_MSforeachdb的知识就介绍到这里了,希望本次的介绍能够对您有所收获!

【编辑推荐】

  1. SQL Server数据库存储过程的异常处理
  2. SQL Server数据库利用SQL语句使用事务详解
  3. SQL Server数据库ROW_NUMBER()函数使用详解
  4. 关闭SQL Server 2005远程连接以及其它对外服务
  5. SQL Server数据库DATEDIFF的参数介绍及使用示例
责任编辑:赵鹏 来源: 博客园
相关推荐

2017-08-09 08:56:04

SP存储Android

2010-01-28 09:13:49

Windows 7SP1SP2

2009-02-06 10:03:08

Vista SP1Vista SP2RC

2009-10-22 09:46:14

CCIE SP

2012-10-12 13:35:51

Windows 8

2010-05-10 10:27:57

CCIE SP Ope

2009-01-20 10:18:00

gOSLinuxGoogle

2009-12-10 10:15:22

VS SP6.0

2013-11-21 13:55:36

Office 2013Office 2013

2013-10-12 09:24:47

Win 7雨林木风补丁

2013-05-17 14:34:40

Win7 SP1XP SP3补丁包

2010-01-21 09:15:17

Windows 7 S开发计划

2011-02-22 09:04:30

Windows 7 S

2010-10-28 16:22:58

Windows 7 S

2010-05-10 10:24:30

CCIE SP Ope

2011-07-05 10:03:22

Vista SP1

2011-05-05 16:22:23

Windows 7 S

2011-07-26 14:17:55

2009-12-28 09:28:38

Windows 7系统升级包

2011-02-23 08:29:21

Windows 7 S
点赞
收藏

51CTO技术栈公众号