如何使用sp_send_dbmail发送多个查询?

时间:2022-11-30 09:35:41

I'm trying to send an email using sp_send_dbmail. I need it to send one query as an attachment and another as part of the body of the email. The problem is that sp_send_dbmail only has one @query parameter, and I can't see any way to add another one.

我正在尝试使用sp_send_dbmail发送电子邮件。我需要它将一个查询作为附件发送,另一个作为电子邮件正文的一部分发送。问题是sp_send_dbmail只有一个@query参数,我看不到任何添加另一个的方法。

Is there any way to either a) add a second query (with all of the parameters attached to it) or b) execute the query into a variable and then add that to the body of my email?

是否有任何方法可以a)添加第二个查询(附加所有参数)或b)执行查询到变量然后将其添加到我的电子邮件正文?

I'm using sql-server 2005.

我正在使用sql-server 2005。

TIA!

1 个解决方案

#1


5  

Use the @query parameter of the stored procedure msdb.dbo.sp_send_dbmail for the attachment and use the @body parameter with a variable that contains the result of the other query.

使用存储过程msdb.dbo.sp_send_dbmail的@query参数作为附件,并将@body参数与包含其他查询结果的变量一起使用。

The example code below creates a string from SQL Server job step history that contains HTML table elements used to send an email using the stored procedure msdb.dbo.sp_send_dbmail. You should be able to adapt it for your purposes.

下面的示例代码从SQL Server作业步骤历史记录创建一个字符串,该字符串包含用于使用存储过程msdb.dbo.sp_send_dbmail发送电子邮件的HTML表元素。您应该能够根据您的目的进行调整。

DECLARE @cat        varchar(MAX),
        @email_id   int


SELECT @cat = COALESCE(@cat + '', '')
                + '<tr><td>'
                + j.[name] + '</td><td>'
                + CAST(js.step_id AS varchar) + '</td><td>'
                + js.step_name + '</td><td>'
                + CONVERT(char(23), jsl.date_created, 121) + '</td><td>'
                + jsl.[log] + '</td></tr>'
FROM    msdb.dbo.sysjobstepslogs jsl
        JOIN msdb.dbo.sysjobsteps js ON jsl.step_uid = js.step_uid
        JOIN msdb.dbo.sysjobs j ON js.job_id = j.job_id


SET @cat = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<style type="text/css">
    td {
        border: 1pt dotted #ddd;
    }

    #log_text {
        width: 20em;
    }
</style>
</head>
<body>
<table>
<colgroup>
    <col />
    <col />
    <col />
    <col />
    <col id="log_text" />
</colgroup>
<thead>
<tr>
<th>Job</th><th>Step</th><th>Step name</th><th>Log created</th><th>Log text</th></tr>
</thead>
<tbody>
        ' + @cat + '
</tbody>
</table>
</body>
</html>'


EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'SQLServerDatabaseMailProfile',
    @recipients = 'person@example.com',
    @subject = 'SQL Server Database Mail · Job step logs',
    @body = @cat,
    @body_format = 'HTML',
    @mailitem_id = @email_id OUTPUT

#1


5  

Use the @query parameter of the stored procedure msdb.dbo.sp_send_dbmail for the attachment and use the @body parameter with a variable that contains the result of the other query.

使用存储过程msdb.dbo.sp_send_dbmail的@query参数作为附件,并将@body参数与包含其他查询结果的变量一起使用。

The example code below creates a string from SQL Server job step history that contains HTML table elements used to send an email using the stored procedure msdb.dbo.sp_send_dbmail. You should be able to adapt it for your purposes.

下面的示例代码从SQL Server作业步骤历史记录创建一个字符串,该字符串包含用于使用存储过程msdb.dbo.sp_send_dbmail发送电子邮件的HTML表元素。您应该能够根据您的目的进行调整。

DECLARE @cat        varchar(MAX),
        @email_id   int


SELECT @cat = COALESCE(@cat + '', '')
                + '<tr><td>'
                + j.[name] + '</td><td>'
                + CAST(js.step_id AS varchar) + '</td><td>'
                + js.step_name + '</td><td>'
                + CONVERT(char(23), jsl.date_created, 121) + '</td><td>'
                + jsl.[log] + '</td></tr>'
FROM    msdb.dbo.sysjobstepslogs jsl
        JOIN msdb.dbo.sysjobsteps js ON jsl.step_uid = js.step_uid
        JOIN msdb.dbo.sysjobs j ON js.job_id = j.job_id


SET @cat = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<style type="text/css">
    td {
        border: 1pt dotted #ddd;
    }

    #log_text {
        width: 20em;
    }
</style>
</head>
<body>
<table>
<colgroup>
    <col />
    <col />
    <col />
    <col />
    <col id="log_text" />
</colgroup>
<thead>
<tr>
<th>Job</th><th>Step</th><th>Step name</th><th>Log created</th><th>Log text</th></tr>
</thead>
<tbody>
        ' + @cat + '
</tbody>
</table>
</body>
</html>'


EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'SQLServerDatabaseMailProfile',
    @recipients = 'person@example.com',
    @subject = 'SQL Server Database Mail · Job step logs',
    @body = @cat,
    @body_format = 'HTML',
    @mailitem_id = @email_id OUTPUT