sp_MSforeachdb获得所有的数据库的存储空间
来源:互联网 发布:东南大学seu网络 编辑:程序博客网 时间:2024/04/29 20:48
/*********************************************************************************
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_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_MSforeachdb获得所有的数据库的存储空间
- 获得数据库中所有的表.
- 【SQLServer】获得当前数据库的所有表
- 获得android下面,所有的数据库
- 获得MySQL数据库所有库的名称
- 与存储过程sp_MSforeachdb类似的存储过程sp_MSforeachdb
- 遍历数据库的未公开存储过程sp_MSforeachdb应用之Attach数据库
- 如何获得mysql数据库的所有的列
- 如何获得mysql数据库的所有的列
- SQL 获得系统所有数据库的所有表以及指定数据库的指定表的字段
- 数据库中汉字所占用的存储空间
- 计算数据库存储空间的jdbc实现
- 如何获得数据库里所有表的名字(转)
- 如何获得数据库里所有表的名字
- 如何获得数据库里所有表的名字
- android查询数据库获得手机里面所有的联系人
- 查询数据库以获得所有 表的索引
- mssql里sp_MSforeachtable和sp_MSforeachdb的用法
- 硬盘安装Fedora 9时死机?
- 高中选择题练习
- Strut&Spring&Hibernate结合开发举例
- 经典哲理故事 陈阿土
- 【转】DataGridView对Access数据库的操作
- sp_MSforeachdb获得所有的数据库的存储空间
- 获取数据库所有表名与字段名
- 对行状态的理解问题
- mapxtreme文件说明
- 悟透JavaScript(李站老师) -引子
- 请帮忙。。。 使用谷歌代码网站上的开源代码,为指定站点提供AJAX定制搜索
- 请求帮助
- sp_MSforeachtable的作用
- 大师忠告