简体   繁体   中英

Msg 102, Level 15, State 1, Line 13 Incorrect syntax near '?'

I am struggling with a small piece of code.

the code runs fine in normal ssms but seems to fail when I put it into a step for mailing?

It just list tables and counts, have been doing it manually for too long. I am trying to create Jobs so as the data is updated, the counts can be mailed to repsective person per server.

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'

Modify it to this below(just add the quotes at the proper places)

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM