sp_send_dbmail will not send query results

对着背影说爱祢 提交于 2019-11-29 19:48:34

问题


I've tried every avenue on every damn forum suggested, but to no avail! Need to send results of SQLPERF(logspace), that have been stored in a table, via sp_send_dbmail to recipient.

Step 2 of job is where failure occurs. Please help!

EXEC msdb.dbo.sp_send_dbmail
@profile_name= 'MyDBA',
@recipients= 'Mack@mydba.co.za',
@subject='Log Warning',
@query='SELECT * from #TempForLogSpace WHERE LogSpaceUsed >80

回答1:


You can't query from a temp table using database mail. The session that you used to create the temp table (step 1 of your job I assume) has been closed and a new session started when step 2 started. Because the session has been closed the table has been dropped (even if the table hasn't been dropped because it's a new session you don't have access to the other sessions temp table).

Either create a physical table and use that (either in the tempdb database or your database) or put the code which creates the output in the @query with the select * from #TempForLogSpace at the end (a stored procedure will be much easier to deal with in this case).




回答2:


I know this thread is a bit old, but in case someone stumbles on this, the problem is like mrdenny said that sp_send_dbmail stored procedure runs in it's own session, however you can get around this by using a global temporary table instead (prefix the table with two pound signs (##)).




回答3:


Just use a global temp table like ##temp_table. This table will be accessible by all sessions and will remain in database until all sessions that referred it have been closed.

Local and global temporary tables in SQL Server



来源:https://stackoverflow.com/questions/1322281/sp-send-dbmail-will-not-send-query-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!