SQL Server邮件变量用法

kle*_*jko 2 sql t-sql sql-server

我想在SQL Server上使用发送邮件实用程序,其中收件人变量(@recipients)应将格式化的字符串作为值.我想在下面展示一个例子:

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'
Run Code Online (Sandbox Code Playgroud)

但是当我执行上面的语句时,会出现错误消息:关键字附近的语法不正确 Right

Lar*_*rnu 5

您不能将表达式作为参数传递给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';
Run Code Online (Sandbox Code Playgroud)