Run same command on all SQL Server databases without cursors
- --This query will return a listing of all tables in all databases on a SQL instance:
- DECLARE @command varchar(1000)
- SELECT @command = 'USE ? SELECT name FROM sysobjects WHERE xtype = ''U'' ORDER BY name'
- EXEC sp_MSforeachdb @command
- --This statement creates a stored procedure in each user database that will return a listing of all users in a database, sorted by their modification date
- DECLARE @command varchar(1000)
- SELECT @command = 'IF ''?'' NOT IN(''master'', ''model'', ''msdb'', ''tempdb'') BEGIN USE ? EXEC(''CREATE PROCEDURE pNewProcedure1 AS SELECT name, createdate, updatedate FROM sys.sysusers ORDER BY updatedate DESC'') END'
- EXEC sp_MSforeachdb @command
- --This query will return a listing of all files in all databases on a SQL instance:
- EXEC sp_MSforeachdb 'USE ? SELECT ''?'', SF.filename, SF.size FROM sys.sysfiles SF'
查询跨库存储过程调用,没有直接的方法:
- DECLARE @sql NVARCHAR(2000)='USE ?
SELECT DB_NAME();- SELECT DISTINCT so.name
- FROM syscomments sc
- INNER JOIN sysobjects so ON sc.id=so.id
- WHERE sc.TEXT LIKE ''%P_ThisIsAStoredProcedure%'''
- EXEC sp_MSforeachdb @sql
联合sp_MSforeachdb,SP_MSFOREACHTABLE,sp_spaceused 查询各库各表的大小:
- DECLARE @sql NVARCHAR(2000)='USE ^;
- --SELECT DB_NAME();
- EXEC sp_spaceused;
- --EXEC SP_MSFOREACHTABLE ''EXEC sp_spaceused "?"''
- CREATE TABLE #t
- (
- name VARCHAR(255),
- ROWS BIGINT,
- reserved VARCHAR(20),
- DATA VARCHAR(20),
- index_size VARCHAR(20),
- unused VARCHAR(20)
- )
- EXEC sp_MSforeachtable "insert into #t exec sp_spaceused ''?''"
- SELECT *,CAST(SUBSTRING(data,0,LEN(data)-2) AS float)/1024 as Data_MB FROM #t ORDER BY Data_MB desc
- DROP TABLE #t
- '
- EXEC sp_MSforeachdb @sql,'^'
查所有数据库表大小:
- create table #temp1
- ([数据库名] varchar(50),
- [数据库大小(MB)] dec (15,2),
- [未分配空间(MB)] dec (15,2),
- [保留(MB)] dec (15,2),
- [数据(MB)] dec (15,2),
- [索引(MB)] dec (15,2),
- [未使用(MB)] dec (15,2))
- insert into #temp1
- exec sp_msforeachdb
- 'use ?;
- select
- db_name(),
- convert(dec (15,2),(convert(dec (15,2),dbsize) + convert (dec (15,2),logsize)) * 8192 / 1048576),
- convert(dec (15,2),(case when dbsize >= reservedpages then (convert (dec (15,2),dbsize) - convert (dec (15,2),reservedpages)) * 8192 / 1048576 else 0 end)),
- convert(dec (15,2),reservedpages * 8192 / 1048576.),
- convert(dec (15,2),pages * 8192 / 1048576.),
- convert(dec (15,2),(usedpages - pages) * 8192 / 1048576.),
- convert(dec (15,2),(reservedpages - usedpages) * 8192 / 1048576.)
- FROM
- (
- SELECT sum(convert(bigint,case when status & 64 = 0 then size else 0 end)) AS dbsize
- , sum(convert(bigint,case when status & 64 <> 0 then size else 0 end)) AS logsize
- from dbo.[sysfiles]
- ) a
- LEFT JOIN
- (
- select sum(a.total_pages) AS reservedpages,
- SUM(a.used_pages) AS usedpages,
- SUM(
- CASE
- -- XML-Index and FT-Index internal tables are not considered "data", but is part of "index_size"
- When it.internal_type IN (202,204,211,212,213,214,215,216) Then 0
- When a.type <> 1 Then a.used_pages
- When p.index_id < 2 Then a.data_pages
- Else 0
- END
- ) AS pages
- from [sys].[partitions] p join [sys].[allocation_units] a on p.partition_id = a.container_id
- left join [sys].[internal_tables] it on p.object_id = it.object_id
- ) b ON 1=1
- '
- select * from #temp1 ORDER BY [数据库大小(MB)] desc
- drop table #temp1
查询数据库某库下所有表的所有字段
- SELECT TOP 100 a.TABLE_NAME,a.COLUMN_NAME,a.DATA_TYPE,a.CHARACTER_MAXIMUM_LENGTH,b.value
- from information_schema.COLUMNS as a left join sys.extended_properties as b
- on a.TABLE_NAME=OBJECT_NAME(b.major_id) and a.ORDINAL_POSITION=b.minor_id
- where a.COLUMN_NAME LIKE '%parcelList%'
Run same command on all SQL Server databases without cursors的更多相关文章
- Describe in brief Databases and SQL Server Databases Architecture.
Databases- A database is a structured collection of data.- Database can be thought as simple data fi ...
- BCP command usage in SQL Server
The bcp Command-Line Utility You use the bcp (bulk copy program) tool to address the bulk movement o ...
- 转:Move all SQL Server system databases at one time
Problem One task that you may need to do as a DBA is to move the system databases from one location ...
- P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1
P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1 May ...
- Microsoft SQL Server Trace Flags
Complete list of Microsoft SQL Server trace flags (585 trace flags) REMEMBER: Be extremely careful w ...
- Migrating Oracle on UNIX to SQL Server on Windows
Appendices Published: April 27, 2005 On This Page Appendix A: SQL Server for Oracle Professionals Ap ...
- Microsoft SQL Server Version List [sqlserver 7.0-------sql server 2016]
http://sqlserverbuilds.blogspot.jp/ What version of SQL Server do I have? This unofficial build ch ...
- Microsoft SQL Server Version List(SQL Server 版本)
原帖地址 What version of SQL Server do I have? This unofficial build chart lists all of the known Servic ...
- Zabbix template for Microsoft SQL Server总结
Zabbix template for Microsoft SQL Server介绍 这里介绍Zabbix下监控Microsoft SQL Server数据库非常好用的一个模板,模板名为&qu ...
随机推荐
- C#设计模式(10)——组合模式(Composite Pattern)
一.引言 在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象 ...
- 设计模式之美:Factory Method(工厂方法)
索引 别名 意图 结构 参与者 适用性 缺点 效果 相关模式 命名约定 实现 实现方式(一):Creator 类是一个抽象类并且不提供它所声明的工厂方法的实现. 实现方式(二):Creator 类是一 ...
- NodeJS Hello world
#2 NodeJS Hello world 打开 https://nodejs.org/api/synopsis.html 将上述代码保存至D:\NODEJS\example.js 打开CMD窗口,定 ...
- 创建Fragment
你可以认为fragment是Activity中模块化的部分.Fragment有它自己的生命周期,接收它自己的输入事件,并且你可以在Activity运行的时候添加或移除它(有点像可以被重用在不同Acti ...
- PAAS平台构建7×24小时高可用应用的方案设计
本博客迁移到部署在jae上的独立博客系统wordpress,博客地址:点击打开独立博客.欢迎大家一起来讨论IT技术. 现在很多企业都在搭建自己的私有PAAS平台,当然也有很多大型互联网公司搭建共有PA ...
- C#与数据库访问技术总结(十四)之DataAdapter对象
DataAdapter对象 DataAdapter对象主要用来承接Connection和DataSet对象. DataSet对象只关心访问操作数据,而不关心自身包含的数据信息来自哪个Connectio ...
- document对象操作:浏览器页面文件
//找元素 1.根据id找 <div id="d1" cs="ceshi"><span>document对象</span>& ...
- MultiTouch————多点触控,伸缩图片,变换图片位置
前言:当今的手机都支持多点触控功能(可以进行图片伸缩,变换位置),但是我们程序员要怎样结合硬件去实现这个功能呢? 跟随我一起,来学习这个功能 国际惯例:先上DEMO免费下载地址:http://down ...
- php学习第一讲----php是什么?
前言:不要在冲动的情况下做任何决定 ——————————————————————————————————————————————————————---- 一.学php之前的一些需要了解的知识 (1)网 ...
- Objective-C 高性能的循环
Cocoa编程的一个通常的任务是要去循环遍历一个对象的集合 (例如,一个 NSArray, NSSet 或者是 NSDictionary). 这个看似简单的问题有广泛数量的解决方案,它们中的许多不乏 ...