【问题标题】:How to list all the user tables in Sybase along with their row count?如何列出 Sybase 中的所有用户表及其行数?
【发布时间】:2013-08-14 06:11:58
【问题描述】:

我想返回所有表格及其旁边的计数。最快的方法是什么?

我知道在 Oracle 中,您可以执行以下操作,但不确定 Sybase:

declare n number;
begin
   for rec in (select object_name from user_objects where object_type='TABLE')
   loop
     execute immediate 'select count(*) from '||rec.object_name into n;
     dbms_output.put_line (rec.object_name||':'||n);
   end loop;
end;

【问题讨论】:

    标签: sybase


    【解决方案1】:

    这是执行上述操作的 Sybase sql:

    select ob.name,st.rowcnt 
    from sysobjects ob, systabstats st 
    where ob.type="U"  
    and st.id=ob.id 
    order by ob.name
    

    【讨论】:

    • 通过运行此查询,我得到了许多表的多个条目。我认为这可能是因为有多个分区。这可能是原因吗?
    • 可能。查看 sysobjects 表了解更多详情。
    • 添加了 group by 子句来处理多个条目:select ob.name, sum(st.rowcnt) from sysobjects ob, systabstats st where ob.type="U" and st.id=ob.id按 ob.name 分组 按 ob.name 排序
    • Sybase IQ 对 Table 'systabstats' not found 做出反应!?!
    • 这给出了一个近似值而不是精确计数。
    【解决方案2】:

    使用下面的查询

    select name,row_count(db_id(),id) as "Rows"
    from sysobjects where type='U' order by 2 desc
    

    【讨论】:

      【解决方案3】:

      这取决于您所指的 Sybase 产品。在我的 SQL Anywhere(9 和 11)中,您的解决方案不起作用,但这是有效的:

      select table_name, count
      from systable
      where primary_root<>0 and creator=1
      order by 1
      

      【讨论】:

      • 很遗憾,systable 已被弃用
      • 我必须使用旧版本的 sybase,而且效果很好!
      • 这只会为我显示以 DBA 作为所有者的表。
      • 它也适用于 SQL Anywhere 16。我以非 DBA 身份登录,它显示了该用户的所有表,如我所愿。
      【解决方案4】:
      select ob.name,st.rowcnt from sysobjects ob, systabstats st  where  b.type='U' 
      and st.id=ob.id  and indid=0 order by ob.name
      

      【讨论】:

      • 这里我还得到“未找到表 'systabstats'”
      • 您可以在答案中添加一些上下文而不是发布纯代码答案吗?
      【解决方案5】:

      如果当前用户是创建者:

      SELECT table_name, count
        FROM sys.systable
       WHERE creator = user_id()
      

      注意:我在 Sybase ASA 9 中对此进行了测试。

      【讨论】:

      • 同样,这只会为我显示以 DBA 作为所有者的表。
      【解决方案6】:

      由于systabstats表中可以有多个条目,查询应该是:

      select ob.name, sum(st.rowcnt)
      from sysobjects ob, systabstats st 
      where ob.type="U"  
      and st.id=ob.id 
      group by ob.name
      order by ob.name
      

      【讨论】:

        【解决方案7】:

        这适用于我在 SQL Anywhere 17 中使用“SQL Central”:

        SELECT table_name, st.count
        FROM systable st 
        WHERE table_type = 'BASE'
        

        【讨论】:

          猜你喜欢
          • 2013-11-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-15
          • 2017-02-21
          • 2013-07-08
          • 1970-01-01
          • 2015-07-03
          相关资源
          最近更新 更多