sqlserver查询数据的所有表名和行数

原文:sqlserver查询数据的所有表名和行数

//查询所有表明select name from sysobjects where xtype=‘u‘

select * from sys.tables

//查询数据库中所有的表名及行数
SELECT  a.name AS [TABLE NAME] ,
        b.rows AS [RECORD COUNT]
FROM    sysobjects AS a
        INNER JOIN sysindexes AS b ON a.id = b.id
WHERE   ( a.type = ‘u‘ )
        AND ( b.indid IN ( 0, 1 ) )
ORDER BY a.name ,
        b.rows DESC
//查询所有的标明及空间占用量\行数

select

object_name(id) tablename,

8*reserved/1024 reserved,

rtrim(8*dpages)+‘kb‘ used,

8*(reserved-dpages)/1024 unused,

8*dpages/1024-rows/1024*minlen/1024 free,

rows

--,*

from sysindexes

where indid=1

order by tablename,reserved desc
SELECT  T.TABLE_NAME AS [TABLE NAME] ,
        MAX(I.ROWS) AS [RECORD COUNT]
FROM    SYSINDEXES I ,
        INFORMATION_SCHEMA.TABLES T
WHERE   T.TABLE_NAME = OBJECT_NAME(I.ID)
        AND T.TABLE_TYPE = ‘BASE TABLE‘
GROUP BY T.TABLE_SCHEMA ,
        T.TABLE_NAME;
---------------------------------------------------------------

SELECT  OBJECT_NAME(id) AS [TABLE NAME] ,
        rowcnt AS [RECORD COUNT]
FROM    sysindexes
WHERE   indid < 2
        AND OBJECTPROPERTY(id, ‘ismsshipped‘) = 0
ORDER BY OBJECT_NAME(id)
 
--创建临时表
CREATE   TABLE ##RowCount
    (
      [TABLE NAME] VARCHAR(500) ,
      [RECORD COUNT] INT
    )
--执行存储过程
EXEC sp_msforeachtable ‘insert   into   ##RowCount   ([TABLE NAME],[RECORD COUNT])   select   ‘‘?‘‘   tableName,   count(*)   dataCount from   ?‘   

--查询结果
SELECT  *
FROM    ##RowCount
ORDER BY [TABLE NAME]  

DROP TABLE ##RowCount 
--统计一个数据库中所有表记录的数量
-- 最近公司的数据库发现有表的数据被弄掉了,有些数据表记录为0,于是想找出此数据库中到底有哪些数据表的记录都为0以缩小分析范围,可使用如下的SQL Statement: 

CREATE   TABLE #tmptb
    (
      tbname sysname ,
      tbrows INT ,
      tbREserved VARCHAR(10) ,
      tbData VARCHAR(10) ,
      tbIndexSize VARCHAR(10) ,
      tbUnUsed VARCHAR(10)
    ) 

INSERT  INTO #tmptb
        EXEC sp_MSForEachTable ‘EXEC   sp_spaceused   ‘‘?‘‘‘ 

SELECT  *
FROM    #tmptb
 --列出所有表的情况 

SELECT  tbrows ,
        tbname
FROM    #tmptb
WHERE   tbrows = 0 --列出记录数据为0的表
ORDER BY tbname

DROP TABLE #tmptb

--其中
--tbname     表名
--tbrows     记录数
--tbREserved     保留空间
--tbData     使用空间
--tbIndexSize     索引使用空间
--tbUnUsed   未用空间
  --SQLServer遍历数据库所有表及统计表数据总数:
  DECLARE @TableName VARCHAR(255);
  CREATE TABLE #GetRecordingTempTable
    (
      [id] [INT] IDENTITY(1, 1)
                 NOT NULL ,
      [TableName] VARCHAR(255) NOT NULL ,
      [RecordingCount] INT
    );
  DECLARE Table_Cursor CURSOR
  FOR
    SELECT  [name]
    FROM    sysobjects
    WHERE   xtype = ‘U‘;
  OPEN Table_Cursor;
  FETCH NEXT FROM Table_Cursor INTO @TableName;
  WHILE ( @@FETCH_STATUS = 0 )
    BEGIN
        EXEC(‘INSERT INTO #GetRecordingTempTable ([TableName],[RecordingCount]) SELECT ‘‘‘+@TableName+‘‘‘, COUNT(0) FROM [‘+@TableName+‘];‘);
        FETCH NEXT FROM Table_Cursor INTO @TableName;
    END
  CLOSE Table_Cursor;
  DEALLOCATE Table_Cursor;
  SELECT    [TableName] AS [表名称] ,
            [RecordingCount] AS [总记录数]
  FROM      #GetRecordingTempTable
  ORDER BY  [TableName];
  DROP TABLE #GetRecordingTempTable;
  GO
时间: 2024-10-24 04:34:42

sqlserver查询数据的所有表名和行数的相关文章

sqlserver查询数据的所有表名和行数及空间占用量

//查询所有表名 select name from sysobjects where xtype='u'SELECT     name, object_id, principal_id, schema_id, parent_object_id, type, type_desc, create_date, modify_date, is_ms_shipped, is_published,                       is_schema_published, lob_data_spa

SQLserver查询数据库所有字段-表名

SELECT * FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='Account' SELECT (case when a.colorder=1 then d.name else '' end) as 表名,--如果表名相同就返回空 a.colorder as 字段序号, a.name as 字段名, (case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' e

EntityFramework检索数据自动将表名变复数问题

问题: EF检索数据自动将表名变复数问题 解决方案: 1.在模型类上添加表名,如:在skusertb类上添加标签:[Table("skusertb")] 代码: namespace Model { using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; [

MySql 查询数据库中所有表名

查询数据库中所有表名select table_name from information_schema.tables where table_schema='csdb' and table_type='base table'; 查询指定数据库中指定表的所有字段名column_nameselect column_name from information_schema.columns where table_schema='csdb' and table_name='users'

使用一个T-SQL语句批量查询数据表占用空间及其行数

原文:使用一个T-SQL语句批量查询数据表占用空间及其行数 要找到数据库中数据表占用的空间和存在的行数.可以使用sp_spaceused搭配数据表的名称.就可以产生该表耗用的空间和现有行数. 如: USE ADVENTUREWORKS GO EXEC sp_spaceused [Sales.SalesOrderHeader] GO 但如果数据库中包含数千的数据表,如何能利用一句SQL语句来实现? 解决方法: 一.动态SQL: 先用T-SQL动态产生表达式,然后放到一个查询中执行.如: USE A

sql server 查询数据库所有的表名+字段

原文:sql server 查询数据库所有的表名+字段 SELECT * FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='Account' SELECT    (case when a.colorder=1 then d.name else '' end) as 表名,--如果表名相同就返回空       a.colorder as 字段序号,       a.name as 字段名,       (case when COLUMNPROPER

查询数据库里所有表名和字段名的语句

查询数据库里所有表名和字段名的语句SQL 查询所有表名:SELECT NAME FROM SYSOBJECTS WHERE TYPE='U'mysql: SELECT * FROM INFORMATION_SCHEMA.TABLES查询表的所有字段名:SELECT NAME FROM SYSCOLUMNS WHERE ID=OBJECT_ID(' 表名' )SELECT * FROM INFORMATION_SCHEMA.TABLESSELECT * FROM INFORMATION_SCHEM

Oracle 查询库中所有表名、字段名、字段名说明,查询表的数据条数、表名、中文表名、

查询所有表名:select t.table_name from user_tables t;查询所有字段名:select t.column_name from user_col_comments t;查询指定表的所有字段名:select t.column_name from user_col_comments t where t.table_name = 'BIZ_DICT_XB';查询指定表的所有字段名和字段说明:select t.column_name, t.column_name from

Oracle 查询库中所有表名、字段名、表名说明、字段名说明(原创)

查询所有表名:select t.table_name from user_tables t;查询所有字段名:select t.column_name from user_col_comments t;查询指定表的所有字段名:select t.column_name from user_col_comments t where t.table_name = 'BIZ_DICT_XB';查询指定表的所有字段名和字段说明:select t.column_name, t.column_name from