简体   繁体   English

xp_sendmail使用表变量作为@query SQL Server 2000

[英]xp_sendmail using table variable as @query SQL Server 2000

I am trying to send a resultset via email using xp_sendmail . 我正在尝试使用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 将查询结果放入表变量/临时表,然后在xp_sendmail中使用

Declare @table_var table(...)

..query execution..

EXEC master.dbo.xp_sendmail @recipients = 'xx@xx.com', 
@query = 'select * from @table_var'

it gives error saying that 它给出了错误的说法

@table_var must be declared. 必须声明@table_var。

Even if I use temporary table, the message I get is 即使我使用临时表,我收到的消息也是

cannot reference object in tempdb database. 无法在tempdb数据库中引用对象。

Any ideas on this? 有什么想法吗?

Thanks in advance 提前致谢

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 = 'xx@xx.com', 
    @query = 'select * from mydatabase.dbo.mytable'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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