Looping over the databases on a server
2016-08-20 18:08
309 查看
SP_MSFOREACHDB
获得所有实例下数据库名称
EXEC sp_MSForEachDB 'PRINT "?"'
sp_MSforeachtable
USE MASTER
GO
SP_HELPTEXT sp_MSforeachtable
这个系统存储过程有7个参数:
@command1 nvarchar(2000), --第一条运行的T-SQL指令
@replacechar nchar(1) = N'?', --指定的占位符号
@command2 nvarchar(2000) = null,--第二条运行的T-SQL指令
@command3 nvarchar(2000) = null, --第三条运行的T-SQL指令
@whereand nvarchar(2000) = null, --可选条件来选择表
@precommand nvarchar(2000) = null, --在表前执行的指令
@postcommand nvarchar(2000) = null --在表后执行的指令
获取库下面所有表和行数
EXEC sp_MSForEachTable 'PRINT ''?''; SELECT ''?'' as [TableName],
COUNT(*) AS [RowCount] FROM ?;'
查看所有表行数,和表空间信息:
EXEC sp_MSForEachTable 'PRINT ''?''; SELECT ''?'' as [TableName],
COUNT(*) AS [RowCount] FROM ?;EXEC SP_SPACEUSED "?"'
更新所有表统计信息:
sp_msforeachtable 'update statistics ?'
获得所有的数据库的存储空间
exec sp_MSforeachdb 'select "?";EXEC sp_spaceused'
所有数据库信息
EXEC sp_MSforeachdb 'USE ? EXEC sp_helpfile;'
更改所有数据库的拥有者为'sa'
EXEC sp_MSforeachdb 'USE ?; EXEC sp_changedbowner ''sa'''
检查数据库完整性
EXEC sp_MSforeachdb 'use ?; DBCC CHECKDB'
删除数据库所有表数据(小心操作)
EXEC sp_MSforeachTABLE 'TRUNCATE TABLE ?'
EXEC sp_MSforeachTABLE "DELETE FROM ?"
获得所有实例下数据库名称
EXEC sp_MSForEachDB 'PRINT "?"'
sp_MSforeachtable
USE MASTER
GO
SP_HELPTEXT sp_MSforeachtable
这个系统存储过程有7个参数:
@command1 nvarchar(2000), --第一条运行的T-SQL指令
@replacechar nchar(1) = N'?', --指定的占位符号
@command2 nvarchar(2000) = null,--第二条运行的T-SQL指令
@command3 nvarchar(2000) = null, --第三条运行的T-SQL指令
@whereand nvarchar(2000) = null, --可选条件来选择表
@precommand nvarchar(2000) = null, --在表前执行的指令
@postcommand nvarchar(2000) = null --在表后执行的指令
获取库下面所有表和行数
EXEC sp_MSForEachTable 'PRINT ''?''; SELECT ''?'' as [TableName],
COUNT(*) AS [RowCount] FROM ?;'
查看所有表行数,和表空间信息:
EXEC sp_MSForEachTable 'PRINT ''?''; SELECT ''?'' as [TableName],
COUNT(*) AS [RowCount] FROM ?;EXEC SP_SPACEUSED "?"'
更新所有表统计信息:
sp_msforeachtable 'update statistics ?'
获得所有的数据库的存储空间
exec sp_MSforeachdb 'select "?";EXEC sp_spaceused'
所有数据库信息
EXEC sp_MSforeachdb 'USE ? EXEC sp_helpfile;'
更改所有数据库的拥有者为'sa'
EXEC sp_MSforeachdb 'USE ?; EXEC sp_changedbowner ''sa'''
检查数据库完整性
EXEC sp_MSforeachdb 'use ?; DBCC CHECKDB'
删除数据库所有表数据(小心操作)
EXEC sp_MSforeachTABLE 'TRUNCATE TABLE ?'
EXEC sp_MSforeachTABLE "DELETE FROM ?"
相关文章推荐
- Windows ->> FIX: “The security database on the server does not have a computer account for this workstation trust relationship”
- List all the Databases on a SQL Server
- The Security Database on the Server Does Not Have a Computer Account
- 108.You are installing Oracle Database 11g on your server. During the installation Oracle Universal
- List all the Databases on a SQL Server
- Install the Domino server 7 on aix 5L 5.2
- [原]Unable to start debugging on the web server.Debugging failes because integrated Windows authentication is not enabled.
- [原]Error while trying to run project: Unable to start debugging on the web server. 内存分配访问无效
- Introducing the Spring Framework --1.(an article posted on TheServerSide.com)
- TF30272: Template not found on the server
- List the Stored Procedures in a SQL Server database
- How to move the databases that are used by SharePoint Portal Server 2003 to a computer that is running SQL Server
- 同步复制错误:The process could not execute 'sp_replcmds' on 'servername'
- How to freeze a dynamic aspx page into a static html page (on the server).
- Build a .NET Application on the Oracle Database
- Unable to start debugging on the web server
- How to freeze a dynamic aspx page into a static html page (on the server).(转:http://codebetter.com/blogs/peter.van.ooijen/archiv
- How to freeze a dynamic aspx page into a static html page (on the server).
- Reporting Services 7: Dynamic ServerReport & the ReportServer Database
- server 2 task(s) are sleeping waiting for space to become available in the log segment for database tempdb.