0

我正在为一小段代码而苦苦挣扎。

代码在正常的 ssms 中运行良好,但当我将其放入邮寄步骤时似乎失败了?

它只是列出表格和计数,手动执行太久了。我正在尝试创建工作,以便在更新数据时,可以将计数邮寄给每台服务器的代表。

EXEC msdb.dbo.sp_send_dbmail 
@profile_name = 'dbmail',
@recipients='random@needsthese.com',
@subject = 'those counts you want',
@body = 'these are count changes',
@execute_query_database = 'nbdb',
@query = 'CREATE TABLE #counts
(
    table_name varchar(255),
    row_count int
)

EXEC sp_MSForEachTable @command1='INSERT #counts (table_name, row_count) 
SELECT ''?'', COUNT(*) FROM ?'

SELECT table_name, row_count FROM #counts ORDER BY table_name, row_count DESC'
4

1 回答 1

0

将其修改为下面的(只需在适当的位置添加引号)

EXEC msdb.dbo.sp_send_dbmail 
@profile_name = 'dbmail',
@recipients='random@needsthese.com',
@subject = 'those counts you want',
@body = 'these are count changes',
@execute_query_database = 'nbdb',
@query = 'CREATE TABLE #counts
(
    table_name varchar(255),
    row_count int
)

EXEC sp_MSForEachTable @command1=''INSERT #counts (table_name, row_count) 
SELECT ''?'', COUNT(*) FROM ?''

SELECT table_name, row_count FROM #counts ORDER BY table_name, row_count DESC'
于 2014-10-20T08:35:10.803 回答