使用sp_MSforeachtable时语法错误不正确

时间:2013-01-08 10:33:04

标签: sql-server tsql sql-server-2000

我计划在sql server 2000中获取所有表的总行数。

我为此做过:

sp_msforeachtable 'select count(*) from ?' 

在这个列中没有提到标题,因为它无法区分哪个行计数属于哪个表

我改变了这个:

sp_msforeachtable 'select count(*) as ? from ?' 

但它抛出一个错误:

Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near '.'.

你能指导一下吗

3 个答案:

答案 0 :(得分:3)

试试这个:

SELECT
    sysobjects.Name, sysindexes.Rows
FROM
    sysobjects
    INNER JOIN sysindexes
    ON sysobjects.id = sysindexes.id
WHERE
    type = 'U'
    AND sysindexes.IndId < 2

答案 1 :(得分:2)

我想我明白了:

exec sp_MSforeachtable 'select count(*) as nr_of_rows, ''?'' 
  as table_name from ?'

答案 2 :(得分:0)

请改为尝试:

sp_MSforeachtable 'select ''?'' Tablename, count(*) ''Rows'' from ?'