【问题标题】:Row count column count in all tables in Sybase databaseSybase 数据库中所有表的行数列数
【发布时间】:2013-07-08 06:06:24
【问题描述】:

我有一个数据库名称 ATs 。在这个数据库中有 150 个表。我想创建一个语句,返回数据库中所有表的行数和列数。

我已经为 SQL SERVER 2008 创建了一个存储过程,但我不知道如何为 Sybase 编写这个脚本。

【问题讨论】:

    标签: sql sybase sybase-asa


    【解决方案1】:

    sys.systable 和 sys.syscolumn 应该为您提供信息:

       Select st.table_name, 
        st.count as row_count,
        col_count = (SELECT count(*) FROM sys.syscolumn where table_id = st.table_id)
        from SYS.SYSTABLE st where st.creator <> 0 and st.count > 0
        order by st.table_name
    

    【讨论】:

      【解决方案2】:

      Sybase ASA 有一组系统表,可为您提供有关数据库结构的信息。您感兴趣的两个表是SYSTABLE(所有表)和SYSCOLUMN(所有列)。

      我尝试了这个适用于我的快速而肮脏的存储过程(在相当陈旧的 ASA 版本 8 上!)。它创建一个临时表和一个游标来遍历所有表。对于每个表,表名、列数和行数都插入到临时表中并最终返回。

      (提示:如果您有很多表,tablefilter 只允许返回整个数据库的子集。)

      CREATE PROCEDURE Usr_TableStats(in par_tablefilter char(100))
      RESULT (tablename varchar(255), number_of_cols int, number_of_rows int)
      BEGIN
      
          declare err_notfound exception for sqlstate value '02000';
          declare @table_id  integer;
          declare @tablename varchar(100);
          declare @cols      integer;
          declare @sql       varchar(300);
      
          declare tables no scroll cursor for select table_id, table_name from sys.systable where table_type = 'BASE' and table_name like par_tablefilter || '%' order by table_name;
      
          create table #tablestats (
              tablename       varchar(100) not null,
              number_of_cols  int not null default 0,
              number_of_rows  int not null default 0
          );
      
          open tables;
      
          LoopTables: loop
      
              fetch next tables into @table_id, @tablename;
      
              if sqlstate = err_notfound then
                  leave LoopTables
              else
                  SELECT COUNT(column_id) INTO @cols FROM SYSCOLUMN WHERE table_id = @table_id;
                  set @sql= 'INSERT INTO #tablestats SELECT ''' || @tablename || ''', ' || @cols || ', COUNT(*) FROM ' || @tablename || ';';
                  EXECUTE IMMEDIATE WITH QUOTES @sql;
              end if
      
          end loop LoopTables;
      
          close tables;
      
          SELECT tablename, number_of_cols, number_of_rows FROM #tablestats;
      
      END
      

      在 iSQL 中这样调用它:

      CALL Usr_TableStats('%'); -- all tables
      CALL Usr_TableStats('ADDRESS%'); -- only tables starting with ADDRESS
      

      【讨论】:

        猜你喜欢
        • 2016-01-15
        • 2015-03-02
        • 1970-01-01
        • 2013-08-14
        • 2017-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-04
        相关资源
        最近更新 更多