如何在sqlserver 2008中一次从所有数据库中获取表的总数 [英] How can i get total count of table from all database at a time in sqlserver 2008

查看:212
本文介绍了如何在sqlserver 2008中一次从所有数据库中获取表的总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hai这是madhu



我的问题是



如何获得表总数在sqlserver 2008中一次从所有数据库(而不是从一个数据库)?



一次从所有数据库返回固定数量的表的方法有多少?



你可以给我回答任何一个...............

解决方案

据我所知,你不可能 - 你甚至没有权限访问其他数据库,所以你很可能列出SQL服务器实例中的所有表。 />


您可以列出所有数据库,然后依次列出每个数据库。

 使用(SqlConnection con =  new  SqlConnection( @ 数据源= GRIFFPC\SQLEXPRESS;集成安全性=真))
{
con.Open() ;
使用(SqlCommand cmd = new SqlCommand( sp_databases,con))
{
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader read = cmd.ExecuteReader();
while (read.Read())
{
Console.WriteLine(( string )读取[ DATABASE_NAME]);
}
}
}

将获取数据库,然后是一个简单的查询:

  SELECT  TABLE_NAME  FROM  INFORMATION_SCHEMA.COLUMNS 

在每个数据库上将重新调整表格名。


Hai this is madhu

my question is

How can i get total count of table from all database (not from one database) at a time in sqlserver 2008?

how many ways are there to return the fixed number of tables from all databases at a time?

can u please give me answer any one ...............

解决方案

As far as I know, you can't - you may not even have permission to access other databases, so it is very, very unlikely that you could list all tables in a SQL server instance.

You could list all the DBs, then list the tables from each in turn.

using (SqlConnection con = new SqlConnection(@"Data Source=GRIFFPC\SQLEXPRESS;Integrated Security=True"))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("sp_databases", con))
        {
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataReader read = cmd.ExecuteReader();
        while (read.Read())
            {
            Console.WriteLine((string)read["DATABASE_NAME"]);
            }
        }
    }

Will get the databases, then a simple query:

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS

On each DB will retun the table names.


这篇关于如何在sqlserver 2008中一次从所有数据库中获取表的总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆