天天看点

SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database)一.本文所涉及的内容(Contents)二.背景(Contexts)三.实现代码(SQL Codes)四.参考文献(References)

<a href="#_labelContents">本文所涉及的内容(Contents)</a>

<a href="#_labelContexts">背景(Contexts)</a>

<a href="#_labelSQLCodes">实现代码(SQL Codes)</a>

<a href="#_labelOne">方法一:游标 + 系统存储过程sp_MSForEachDB</a>

<a href="#_labelTwo">方法二:封装sp_MSforeachtable + sys.databases</a>

<a href="#_labelThree">方法三:系统存储过程sp_MSForEachDB + sp_MSforeachtable</a>

<a href="#_labelFour">方法四:扩展sp_MSforeachdb + sp_MSforeachtable</a>

<a href="#_labelReferences">参考文献(References)</a>

SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database)一.本文所涉及的内容(Contents)二.背景(Contexts)三.实现代码(SQL Codes)四.参考文献(References)

(Figure1:某数据库所有表信息)

SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database)一.本文所涉及的内容(Contents)二.背景(Contexts)三.实现代码(SQL Codes)四.参考文献(References)

(Figure2:所有数据库所有表信息)

下面内容讲述了在实现Figure2过程中遇到的一些问题,如果你对这些问题不感兴趣可以直接看最后实现的SQL脚本。下面讲述了4种实现方法:

  1. 游标 + 系统存储过程sp_MSForEachDB,实现脚本为Script3;

  2. 封装sp_MSforeachtable + sys.databases,实现脚本为Script4和Script5;

  3. 系统存储过程sp_MSForEachDB + sp_MSforeachtable,实现脚本为Script6;

  4. 扩展sp_MSforeachdb + sp_MSforeachtable,实现脚本为Script7;

  1) 既然有了获取某个数据库所有表信息的脚本,那就可以在外层再套使用sp_MSForEachDB系统存储过程,下面的Script1脚本可以获取到所有数据库的所有表的信息,效果如Figure3所示:

SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database)一.本文所涉及的内容(Contents)二.背景(Contexts)三.实现代码(SQL Codes)四.参考文献(References)

(Figure3:所有数据库所有表)

  2) 上图Figure3有两个缺点,第一是返回的数据太分散,没有统一表进行管理,第二是需要过滤master、model、msdb和tempdb等系统数据库,因为我们完全不关心系统数据库,下面的SQL脚本展示在使用sp_msforeachdb的时候如何排除某个数据库,效果如Figure4所示:

SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database)一.本文所涉及的内容(Contents)二.背景(Contexts)三.实现代码(SQL Codes)四.参考文献(References)

(Figure4:sp_msforeachdb排除某个数据库)

  3) 下面的SQL脚本展示在使用sp_msforeachdb的时候如何排除多个数据库,效果如Figure5所示:

SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database)一.本文所涉及的内容(Contents)二.背景(Contexts)三.实现代码(SQL Codes)四.参考文献(References)

(Figure5:sp_msforeachdb排除多个数据库)

  4) 把上面的SQL脚本运用到之前获取某个数据库表信息的SQL脚本中,但是执行的过程中出现了Figure6的错误信息:

SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database)一.本文所涉及的内容(Contents)二.背景(Contexts)三.实现代码(SQL Codes)四.参考文献(References)

(Figure6:错误信息)

  5) 经过一番查找,最后发现是因为AdventureWorksLT2008R2数据库的安全中的架构是SalesLT,不是默认的dbo,所以报了Figure6的错误信息,但是如果使用sp_MSforeachtable,那就不用理会框架的问题。

SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database)一.本文所涉及的内容(Contents)二.背景(Contexts)三.实现代码(SQL Codes)四.参考文献(References)

(Figure7:SalesLT架构名)

只要我们在表名称前面加入正确的架构名,那就可以正确执行了,如Figure8所示:

SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database)一.本文所涉及的内容(Contents)二.背景(Contexts)三.实现代码(SQL Codes)四.参考文献(References)

(Figure8:正确的架构名)

  6) 经过上面经验的总结,关于所有数据库所有表的信息的SQL脚本就水到渠成了,下面就是全部的SQL脚本,注意过滤的方式可以写成:IF ''?'' like(''A%'') ,执行的效果如Figure2所示:

<a href="http://blog.csdn.net/lee576/article/details/1327801">与存储过程sp_MSforeachdb类似的存储过程sp_MSforeachdb</a>

<a href="http://www.searchdatabase.com.cn/showcontent_22780.htm">SQL Server数据库开发顶级技巧</a>

<a href="http://www.cnblogs.com/acis_/archive/2009/07/28/1532614.html">sp_MSforeachtable使用方法</a>

<a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas">How to get information about all databases without a loop</a>

<a href="http://www.cnblogs.com/smfish007bin/archive/2008/10/16/1312786.html">关于quotename的用法</a>

<a href="http://www.cnblogs.com/gaizai/archive/2013/05/07/3064489.html">SQL Server 游标运用:查看一个数据库所有表大小信息(Sizes of All Tables in a Database)</a>