msdb.dbo.sp_send_dbmail temp table does not exist I have created a temp table and would like to email the results I am getting the following error.  


  Query execution failed: Msg 208, Level 16, State 1, Server
  PRODYCHDBX3\X3V7, Line 1 Invalid object name '#WODups'.


After the temp table is created I ran the following code. 

 DECLARE             @recordcount INT
 SELECT @recordcount = ISNULL(COUNT(*),0) 
 FROM #WODups

 IF (@recordcount &gt; 0)

 BEGIN

 SELECT * FROM #WODups
 EXEC msdb.dbo.sp_send_dbmail
 @profile_name = 'SQL Mail',
 @recipients = 'jXXn.XXXn??@yXXXXXX.com;XXXXXXXX1@gmail.com',           
 @query = 'SELECT FROM #WODups',

 @subject = 'Duplicate Work Order Production Tracking',
 --@attach_query_result_as_file = 1,
 @body =0;

 DROP TABLE #WODups


Why am I getting this error, and how may I resolve it?
Personally, when I email results (and it's not 100K rows or whatever) I like to mark it up with CSS and send it as HTML so it is readable. Shnugo has a great function for doing this over on Stackoverflow. 

To use it, aside from his instructions, you can do something like so:

declare @body xml = 
    (select dbo.ufn_CreateHTMLTable (
        (SELECT FROM #WODups
         for xml path ('row'),elements xsinil)
    ,null,null,null))

--Maybe add some borders, Shnugo shows how to do it with his funciton too

declare @body_html varchar(max)
set @body_html =  
    '&lt;style type="text/css" media="screen,print"&gt;
    .center
    {
        text-align: center;
    }
    table,th
    {
        border: 1px solid black;
    }
    table,tr
    {
        border: 1px solid black;
    }
    table,td
    {
        border: 1px solid black;
    }
    &lt;/style&gt;' 
    + cast(@body as varchar(max))

    exec msdb..sp_send_dbmail
    @profile_name = 'SQL Mail', 
    ,@recipients = 'someone@domain.com'
    ,@subject = 'This is my Subject'
    ,@body = @body_html
    ,@body_format = 'HTML'