sp_MSforeachtable使用like跳过某些表名

时间:2016-02-25 09:00:38

标签: sql sql-server-2012

我试图跳过一些内部表格,这些表格的前缀我写的如下,但它不起作用。下面我想跳过所有以xxx_开头的表格。

有什么想法吗?非常感谢你的时间!

DECLARE @TableRowCounts TABLE ([TableName] VARCHAR(128), [RowCount] INT) ;
INSERT INTO @TableRowCounts ([TableName], [RowCount])
EXEC sp_MSforeachtable 'SELECT ''?'' [TableName], COUNT(*) [RowCount] FROM ? WHERE ''?'' NOT  LIKE ''xxx_%''' ;
SELECT [TableName], [RowCount]
FROM @TableRowCounts
ORDER BY [TableName]

1 个答案:

答案 0 :(得分:1)

来自raresql

create table #counts 
(
    table_name varchar(255),
    row_count int
)

EXEC sp_MSForEachTable @command1='INSERT #counts (table_name, row_count) SELECT ''?'', COUNT(*) FROM ?'
, @whereand = ' And Object_id In (Select Object_id From sys.objects
Where name not like ''xxx%'')'
SELECT table_name, row_count FROM #counts