如何使用concat创建字符串以分配给变量

时间:2015-06-04 18:13:49

标签: sql-server stored-procedures sp-send-dbmail

我正在使用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'

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

1 个答案:

答案 0 :(得分: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'