SQL Server Mail变量用法



我想在sql server上使用发送邮件实用程序,其中收件人变量(@recipient)应将格式的字符串作为值。我想在下面显示一个示例:

EXEC msdb.dbo.sp_send_dbmail  
    @profile_name = 'Mail Profile',
    @recipients = Right('domain/user@company.com',
                         CHARINDEX('/',reverse('domain/user@company.com'))-1),
    @body = 'TEST MESSAGE',
    @subject = 'Automated Success Message'

但是,当我执行上述语句时,出现错误消息:不正确的语法Right

您不能将表达式传递给AM SP作为参数。您必须传递字面价值或变量。您需要先声明一个变量,设置其值,然后将其传递为参数:

DECLARE @recipients nvarchar(255);
SET @recipients = RIGHT('domain/user@company.com', CHARINDEX('/', REVERSE('domain/user@company.com')) - 1);
EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Mail Profile',
                             @recipients = @recipients,
                             @body = 'TEST MESSAGE',
                             @subject = 'Automated Success Message';

最新更新