0

I am trying to send a resultset via email using xp_sendmail. I need to send the email when an earlier executed query has any results.

Got the query results into a table variable/temp table and then in xp_sendmail, using

Declare @table_var table(...)

..query execution..

EXEC master.dbo.xp_sendmail @recipients = '[email protected]', 
@query = 'select * from @table_var'

it gives error saying that

@table_var must be declared.

Even if I use temporary table, the message I get is

cannot reference object in tempdb database.

Any ideas on this?

Thanks in advance

1 Answer 1

2

You'll need to use a real table for this. Try..

 If exists (select * from sys.tables where name = 'mytable')
      drop table mytable
    Create Table mytable table(...)


    ..query execution..

    EXEC master.dbo.xp_sendmail @recipients = '[email protected]', 
    @query = 'select * from mydatabase.dbo.mytable'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.