`
shangjava
  • 浏览: 1189050 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

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

SQL 
阅读更多
/*********************************************************************************
 sp_MSforeachdb 系统存储过程有6个参数:
@command1 nvarchar(2000) --第一条运行的t-sql指令
@replacechar nchar(1) = N'?' --指定的占位符号
@command2 nvarchar(2000) = null --第二条运行的t-sql指令
@command3 nvarchar(2000) = null--第三条运行的t-sql指令
@precommand nvarchar(2000) = null--在sp_MSforeach_worker前执行的指令
@postcommand nvarchar(2000) = null --在sp_MSforeach_worker后执行的指令
************************************************************************************/

/*
* The following table definition will be created by SQLDMO at start of each connection.
* We don't create it here temporarily because we need it in Exec() or upgrade won't work.
*/

create proc sp_MSforeachdb
@command1 nvarchar(2000), @replacechar nchar(1) = N'?', @command2 nvarchar(2000) = null, @command3 nvarchar(2000) = null,
@precommand nvarchar(2000) = null, @postcommand nvarchar(2000) = null
as
set deadlock_priority low

/* This proc returns one or more rows for each accessible db, with each db 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 @inaccessible nvarchar(12), @invalidlogin nvarchar(12), @dbinaccessible nvarchar(12)
select @inaccessible = ltrim(str(convert(int, 0x03e0), 11))
select @invalidlogin = ltrim(str(convert(int, 0x40000000), 11))
select @dbinaccessible = N'0x80000000'/* SQLDMODbUserProf_InaccessibleDb; the negative number doesn't work in convert() */
if (@precommand is not null)
exec(@precommand)

declare @origdb nvarchar(128)
select @origdb = db_name()

/* If it's a single user db and there's an entry for it in sysprocesses who isn't us, we can't use it. */
/* Create the select */
exec(N'declare hCForEach cursor global for select name from master.dbo.sysdatabases d ' +
N' where (d.status & ' + @inaccessible + N' = 0)' +
N' and ((DATABASEPROPERTY(d.name, ''issingleuser'') = 0 and (has_dbaccess(d.name) = 1)) or ' +
N' ( DATABASEPROPERTY(d.name, ''issingleuser'') = 1 and not exists ' +
N' (select * from master.dbo.sysprocesses p where dbid = d.dbid and p.spid <> @@spid)))' )

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)

declare @tempdb nvarchar(258)
Select @tempdb = REPLACE(@origdb, N']', N']]')
exec (N'use ' + N'[' + @tempdb + N']')

return @retval



usage:

sp_msforeachdb除了@whereand外,和sp_msforeachtable的参数是一样的,我们可以通过这个存储过程检测所有的数据库,比如:
  1.获得所有的数据库的存储空间:
  exec sp_msforeachdb @command1="print '?'",
  @command2="sp_spaceused "
  2.检查所有的数据库
  exec sp_msforeachdb @command1="print '?'",
  @command2="dbcc checkdb (?) "
分享到:
评论

相关推荐

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

    1.简介: 作为DBA会经常需要检查所有的数据库或用户表,比如:检查所有数据库的容量;看看指定数据库所有用户表的容量,所有表的记录数…,我们一般处理这样的问题都是用游标分别处理处理,比如:在数据库检索效率非常慢时,...

    SQL Server数据表和数据库

    本文介绍了master数据库中两个非常有用的存储过程:sp_MSForEachDB和sp_MSForEachTable。

    SQL Server未公开的两个存储过程

    Sp_MSForEachTable和sp_MSForEachDb是SQL Server的两个系统存储过程,存在于Master数据库当中

    如何对SQL数据表和数据库进行迭代操作

    本文介绍了master数据库中两个非常有用但在SQL Server在线教科书中没有提到的存储过程:sp_MSForEachDB、sp_MSForEachTable。这些系统过程对于处理以下任务非常方便,如判断使用的存储空间大小、行数、用户表索引...

    参考sql2012存储过程写的统计所有用户表尺寸大小的示例

    可以结合sp_MSforeachdb再遍历所有用户数据库查看所有表的尺寸大小,注意它的参数@sql不能超过nvarchar(2000),这里就不贴出代码了。另外还可以定期运行并将结果保存下来,以便观察数据变化趋势。 查询单个数据库的...

    查询数据库状态

    查询数据库的状态信息: ------------------------------Data file size---------------------------- if exists (select * from tempdb.sys.all_objects where name like '%#dbsize%') drop table #dbsize create ...

    sqlserver数据库迁移后,孤立账号解决办法

    ].sys.sp_change_users_login @Action = ”Auto_Fix” , @UserNamePattern = ”qa” , @LoginName = null , @Password = ”abc” ‘ exec sp_msforeachdb @cmd 您可能感兴趣的文章:如何将Oracle的一个大数据表快速...

Global site tag (gtag.js) - Google Analytics