0

我正在使用 sp_send_dbmail 存储过程,并且试图将查询的结果包含在正文字符串中。我似乎无法让它工作。任何想法将不胜感激。

use msdb
go

exec sp_send_dbmail
    @profile_name = 'WarehouseEmailer',
    @recipients = 'someone@example.com',
    @subject = 'Database Mail Test 1',
    @Body = 'This is a test email from SQL Server. <br> This should be line 2. <br> The 
    subject\'s first name is: '+ (select top(1) FirstName from Warehouse.dbo.Subject),
    @body_format = 'HTML'

显然这个例子只是一个简单的测试,但它正是我想传递给存储过程以包含在电子邮件正文中的数据类型。我期待您的回复,非常感谢您的任何建议!

4

1 回答 1

0

动态 SQL 或连接的字符串不能像那样传递到过程参数中。

尝试这个:

use msdb
go
declare @msgbody nvarchar(1000)
set @msgbody = 'This is a test email from SQL Server.' + char(10) + 'This should be line 2. <br> The 
    subject''s first name is: '+ (select top(1) FirstName from Warehouse.dbo.Subject)

exec sp_send_dbmail
    @profile_name = 'WarehouseEmailer',
    @recipients = 'someone@example.com',
    @subject = 'Database Mail Test 1',
    @Body = @msgbody,
    @body_format = 'HTML'
于 2015-06-04T19:08:26.433 回答