【问题标题】:How do I send the result set from a query as an attachment in an email using SSIS?如何使用 SSIS 将查询的结果集作为电子邮件的附件发送?
【发布时间】:2010-12-02 14:47:13
【问题描述】:

我有一个来自 Execute SQL Task 查询的结果集保存在 System.Object 变量中,我想使用 Send Mail Task 在 MessageSource 上使用以下表达式发送此结果。

"请查找附加数据摘要\n\n" + SUBSTRING(@[User::myVariable] ,1,3990)
+ "\n\n"

【问题讨论】:

    标签: sql-server ssis


    【解决方案1】:

    您现在可能已经找到了问题的答案。这个答案适用于可能偶然发现这个问题的其他人。我认为您不能在表达式中使用对象变量。您需要遍历查询结果对象并将其格式化为字符串,以便您可以在电子邮件中发送查询输出。您还可以将数据导出到文件并将文件作为附件发送。那是另一种可能的选择。这个例子展示了如何遍历查询结果集以形成消息正文,然后使用发送电子邮件任务通过电子邮件发送。

    分步过程:

    1. 使用 SQL 脚本 部分下提供的脚本创建一个名为 dbo.EmailData 的表。

    2. 屏幕截图 #1 显示了 Execute SQL 任务将查询并在此示例中通过电子邮件发送的示例数据。

    3. 在 SSIS 包上,创建 5 个变量,如屏幕截图 #2 所示。

    4. 在 SSIS 包上,放置以下任务:Execute SQL taskForeach loop containerScript task Foreach 循环容器Send Email task

      李>
    5. 如屏幕截图 #3 和 #4 所示配置 Execute SQL task

    6. 如屏幕截图 #5 和 #6 所示配置 Foreach loop container。变量映射部分显示查询结果列的显示顺序以及它们如何分配给 SSIS 变量。这些变量将用于在Script task 中形成电子邮件。

    7. Script task 中,将代码替换为脚本任务代码 部分下显示的代码。脚本任务具有非常简单的纯文本电子邮件格式。

    8. 如屏幕截图 #7 所示配置发送电子邮件任务。您需要在 FromTo 字段中使用有效的电子邮件地址对其进行配置。

    9. 配置控制流任务后,您的包应如屏幕截图 #8 所示。

    10. 示例包执行如屏幕截图 #9 所示。

    11. 包裹发送的邮件如截图#10所示。部分信息已从屏幕截图中删除。您可以将屏幕截图 #1 中显示的表格数据与此电子邮件输出进行比较,它们应该相同。

    希望对您有所帮助。

    SQL 脚本: .

    CREATE TABLE [dbo].[EmailData](
        [Id] [int] IDENTITY(1,1) NOT NULL,
        [ItemId] [varchar](255) NOT NULL,
        [ItemName] [varchar](255) NOT NULL,
        [ItemType] [varchar](255) NOT NULL,
        [IsProcessed] [bit] NULL,
     CONSTRAINT [PK_EmailData] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY]
    GO
    

    脚本任务代码:

    C# 代码只能在 SSIS 2008 and above 中使用。 .

    /*Microsoft SQL Server Integration Services Script Task
       Write scripts using Microsoft Visual C# 2008.
       The ScriptMain is the entry point class of the script.
    */
    
    using System;
    using System.Data;
    using Microsoft.SqlServer.Dts.Runtime;
    using System.Windows.Forms;
    
    namespace ST_7f59d09774914001b60a99a90809d5c5.csproj
    {
        [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
        public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
        {
    
            #region VSTA generated code
            enum ScriptResults
            {
                Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
                Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
            };
            #endregion
    
            public void Main()
            {
                Variables varCollection = null;
                string header = string.Empty;
                string message = string.Empty;
    
                Dts.VariableDispenser.LockForWrite("User::EmailMessage");
                Dts.VariableDispenser.LockForWrite("User::ItemId");
                Dts.VariableDispenser.LockForWrite("User::ItemName");
                Dts.VariableDispenser.LockForWrite("User::ItemType");
                Dts.VariableDispenser.GetVariables(ref varCollection);
    
                //Set the header message for the query result
                if (varCollection["User::EmailMessage"].Value == string.Empty)
                {
                    header = "Execute SQL task output sent using Send Email Task in SSIS:\n\n";
                    header += string.Format("{0}\t{1}\t\t\t{2}\n", "Item number", "Item name", "Item type");
                    varCollection["User::EmailMessage"].Value = header;
                }
    
                //Format the query result with tab delimiters
                message = string.Format("{0}\t{1}\t{2}",
                                            varCollection["User::ItemId"].Value,
                                            varCollection["User::ItemName"].Value,
                                            varCollection["User::ItemType"].Value);
    
                varCollection["User::EmailMessage"].Value = varCollection["User::EmailMessage"].Value + message;
    
                Dts.TaskResult = (int)ScriptResults.Success;
            }
        }
    }
    

    屏幕截图 #1:

    屏幕截图 #2:

    截图#3:

    屏幕截图 #4:

    屏幕截图 #5:

    屏幕截图 #6:

    截图#7:

    截图#8:

    屏幕截图 #9:

    屏幕截图 #10:

    【讨论】:

    • 你真好。谢谢。
    【解决方案2】:

    经过多次尝试,这种方法确实有效。但是,如果您有 Visual Studio 2010 或更高版本,则需要稍微更改 C# 脚本。

    替换:

    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    

    用这个:

    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
    

    仍在尝试整理我收到的电子邮件中的间距,但对结果非常满意。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多