原贴: https://www.mssqltips.com/sqlservertip/1414/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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 转: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 ...

  4. 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 ...

  5. Microsoft SQL Server Trace Flags

    Complete list of Microsoft SQL Server trace flags (585 trace flags) REMEMBER: Be extremely careful w ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Zabbix template for Microsoft SQL Server总结

      Zabbix template for Microsoft SQL Server介绍   这里介绍Zabbix下监控Microsoft SQL Server数据库非常好用的一个模板,模板名为&qu ...

随机推荐

  1. 关于mysql占用CPU过高,问题解决

    使用SHOW PROCESSLIST 查看 原因: 使用了 一个触发器 不断的去删除日志,保证每个用户的日志只有10条 去掉之后,CPU使用率从97% 降到了 %. 利用show columns fr ...

  2. [转]轻松解决oracle11g 空表不能exp导出的问题

    转自:http://www.2cto.com/database/201109/105931.html oracle11g的新特性,数据条数是0时不分配segment,所以就不能被导出. 解决方法: 1 ...

  3. C#之Action

    Action<T> 委托 class Program { static void Main(string[] args) { MyDelegate<string>(MyFunc ...

  4. C#函数式编程之缓存技术

    缓存技术 该节我们将分成两部分来讲解,第一部分为预计算,第二部分则为缓存.缓存这个技术对应从事开发的人员来说是非常熟悉的,从页面缓存到数据库缓存无处不在,而其最重要的特点就是在第一次查询后将数据缓存, ...

  5. 在线教学、视频会议 Webus Fox(3) 客户端开发手册

    本文主要介绍webus fox 客户端的配置及接口说明. 1. 文件列表和配置 1.1 文件列表 1.2 common.xml 配置 根据服务器端的部署, 替换[ServerUrl] , [RtmpP ...

  6. 用c#开发微信 (7) 微渠道 - 推广渠道管理系统 2 业务逻辑实现

    我们可以使用微信的“生成带参数二维码接口”和 “用户管理接口”,来实现生成能标识不同推广渠道的二维码,记录分配给不同推广渠道二维码被扫描的信息.这样就可以统计和分析不同推广渠道的推广效果. 上次介绍了 ...

  7. UML建模语言入门 -- 用例视图详解 用例视图建模实战

    . 作者 :万境绝尘  转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/18964835 . 一. 用例视图概述 用例视图表述哪些 ...

  8. [ACM_水题] UVA 11292 Dragon of Loowater [勇士斗恶龙 双数组排序 贪心]

    Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The shor ...

  9. [游戏模版16] Win32 飞机射击 敌人追踪

    >_<:AI introduction. >_<:According the plane position (nowX,nowY) relative to birds' pos ...

  10. JSLint JavaScript代码质量审查工具汉化中文版隆重发布

    JSLint是一款JavaScript代码质量审查工具,它可以指出代码中错误.不规范的地方,非常之严格,甚至多写一个空格都会发出警告. JSLint的审查规则,根据众多前辈多年编程经验而写,字字珠玑, ...