trying to output the result from a sproc with sp_send_dbmail (Thanks to Joe Stefanneli for solution). When using a global Temp table variable (##Summary) it works. When using a local Temp table variable (#Summary) it fails. From what I have read, the global variable option is not applicable here as the sproc will be called by a web application so there will concurrent references to the same temporary table.
Apparently the call to sp_send_dbmail is not 'inner context' so the local variable fails. Is there a solution to this?
Sproc flow: Create Table #Summary (fields...)
Select stuff into #Summary
exec msdb.dbo.sp_send_dbmail
@profile_name = 'Me',
@recipients = 'me@mysite.co.nz',
@body = 'Test from Me',
@subject = 'Automated Test Message',
@query = 'select * from #Summary ' ,
@attach_query_result_as_file = 1,
@query_attachment_filename = 'testing.csv',
@query_result_separator=','
drop table #Summary
thanks