我想结合使用sp_MSforeachtable
和openquery
来获取列表,并从链接服务器获取计数:
除了最后一个命令以外的所有命令到目前为止
从一张表中获取计数:
SELECT *
FROM openquery(ACTIFIODB,'SELECT COUNT(*) AS TOTAL FROM public.applicationvw')
SELECT * FROM sys.servers
从数据库中的所有表中获取计数:
CREATE TABLE #counts
(
table_name varchar(255),
row_count int
)
EXEC sp_MSforeachtable @command1='INSERT #counts (table_name, row_count) SELECT ''?'', COUNT(*) FROM ?'
SELECT *
FROM #counts ORDER BY table_name, row_count DESC
需要将两者合并为以下内容:
SELECT *
FROM openquery(ACTIFIODB, 'EXEC sp_MSforeachtable @command1='INSERT #counts (table_name, row_count) SELECT ''?'', COUNT(*) FROM ?'')