我有10个表,我现在要统计每个表的条数分别显示在同一个页面上(这样的话就要连10次数据库),可不可以用视图把10个表统计出来的数据合在一个表里面这样前台操作就方便多了

解决方案 »

  1.   

    每个表的条数?条数是指的记录总数?select 'table1' as tablename,count(*) as rcount from table1
    union
    select 'table2' as tablename,count(*) as rcount from table2
    union
    select 'table3' as tablename,count(*) as rcount from table3
    union
    .....
      

  2.   

    select a1.cnt , a2.cnt , a3.cnt , a4.cnt,a5.cnt,a6.cnt,a7.cnt,a8.cnt,a9.cnt,a10.cnt
    from
    (select count(*) as cnt from tb1) a1 , 
    (select count(*) as cnt from tb2) a2 , 
    (select count(*) as cnt from tb3) a3 , 
    (select count(*) as cnt from tb4) a4 , 
    (select count(*) as cnt from tb5) a5 , 
    (select count(*) as cnt from tb6) a6 , 
    (select count(*) as cnt from tb7) a7 , 
    (select count(*) as cnt from tb8) a8 , 
    (select count(*) as cnt from tb9) a9 , 
    (select count(*) as cnt from tb10) a10
      

  3.   

    提示:
    declare hcforeach cursor global for
    select name from sysobjects where xtype='U' and name in(你需要显示的表)
    exec sp_msforeach_worker 'select 表名=''?'',count(*) from ?'
      

  4.   

    coolingpipe(冷箫轻笛) 的是把10个结果显示在1列中了,我要的是每个数据在1列里面
      

  5.   

    select 
    col1=(select count(*) from table1),
    col2=(select count(*) from table2), 
    col3=(select count(*) from table3), 
    col4=(select count(*) from table4), 
    col5=(select count(*) from table5), col6=(select count(*) from table6), 
    col7=(select count(*) from table7), 
    col8=(select count(*) from table8), 
    col9=(select count(*) from table9), 
    col10=(select count(*) from table10)
      

  6.   

    if object_id('pubs..mycnt') is not null
       drop view mycnt
    goCREATE VIEW mycnt
    AS
    select 说明 = '第一个表' , count(*) as 数量 from authors
    union all
    select 说明 = '第二个表' , count(*) as 数量 from employee
    goselect * from mycntdrop view mycnt说明       数量          
    -------- ----------- 
    第一个表     23
    第二个表     43(所影响的行数为 2 行)
      

  7.   

    上面是纵向.
    下一个是横向if object_id('pubs..mycnt') is not null
       drop view mycnt
    goCREATE VIEW mycnt
    AS
    select a.数量 as 第一表数量 , b.数量 as 第二表数量 from
    (select count(*) as 数量 from authors) a,
    (select count(*) as 数量 from employee ) b
    goselect * from mycntdrop view mycnt第一表数量  第二表数量       
    ----------- ----------- 
    23          43(所影响的行数为 1 行)
      

  8.   

    其实就是union那些count(*)就可以了,系统表都用不到。