我已经将以下代码放在一起,似乎工作得很好。但是,我需要将它放在存储过程中,但出现以下错误:
过程、函数或触发器中不允许使用 use 语句
有人知道解决方法吗?
CREATE TABLE #TableRowCounts1
(
[TableName] VARCHAR(128),
[RowCount] INT
)
CREATE TABLE #TableRowCounts2
(
[TableName] VARCHAR(128),
[RowCount] INT
)
use Database1
GO
INSERT INTO #TableRowCounts1
(
[TableName],
[RowCount]
)
EXEC sp_MSforeachtable
'SELECT ''?''
[TableName],
COUNT(*) [RowCount]
FROM ?'
use Database2
GO
INSERT INTO #TableRowCounts2
(
[TableName],
[RowCount]
)
EXEC sp_MSforeachtable
'SELECT ''?''
[TableName],
COUNT(*) [RowCount]
FROM ?'